3

I'd like to know, how to calculate integer values of strings in single quotes ' '.

My sample code is:

#include <stdio.h>

int main()
{
    int c = 'aA';
    int d = 'Aa';

    printf( "%d %d" , c, d);

    return 0;
}

And the output is:

24897 16737

What are those numbers? Is there any formula to calculate them ?

4

3 回答 3

5

这些是:

  1. 不是字符串!

  2. 多字节整数,其值由实现定义,但通常使用以下公式计算:

    integer value of 1st character multiplied by (2 << CHAR_BITS) + integer value of 2nd character

因此,假设您的 C 语言环境使用 ASCII 并且您有 8 位字节,'aA'则变为

97 * 256 + 65

这是24897。

多字符文字属于int.

于 2012-11-14T18:16:26.803 回答
2

它是存储在变量中的多字符字符的值

于 2012-11-14T18:16:30.317 回答
1

The value of a multi-character constant is implementation-defined.

§ 6.4.4.4 Character constants
The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

于 2012-11-14T18:24:02.083 回答