In C a character literal (constant) has type int.
So, consider the following program
#include <stdio.h>
main(int argc, char *argv[])
{
printf("%zu
", sizeof( a ));
printf("%zu
", sizeof( ab ));
printf("%zu
", sizeof( abc ));
printf("%zu
", sizeof( abcd ));
printf("%u
", a );
printf("%u
", ab );
printf("%u
", abc );
printf("%u
", abcd );
printf("%x
", a );
printf("%x
", ab );
printf("%x
", abc );
printf("%x
", abcd );
printf("%c
", a );
printf("%c
", ab );
printf("%c
", abc );
printf("%c
", abcd );
}
The first four statements all consider the literals as one character constant and they all print 4 == sizef(int), at least on gcc (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3.
Note that this compiler prints several warnings for the above program:
warning: multi-character character constant
Basically, a character literal specifies the four bytes making up an int,
from left to right, higher-order byte first.
The missing leading bytes are filled with 0.
So, on my machine the second and third group of printf statements print
97
24930
6382179
1633837924
61
6162
616263
61626364
In the hexadecimal output you see the layout of the four characters in the
literal (the ASCII codes from left to right): the a is mapped to the
highest-order byte 0x61).
最后,第四组指纹:
a
b
c
d
i.e. the character literals are pushed on the stack as integers, but printf
only prints the lowest byte of that int as a char.
C++ behaves in a similar way, but one-byte character literals are considered
of type char, not int. The program
#include <iostream>
using namespace std;
main(int argc, char *argv[])
{
cout << sizeof( a ) << endl;
cout << sizeof( ab ) << endl;
cout << sizeof( abc ) << endl;
cout << sizeof( abcd ) << endl;
cout << a << endl;
cout << ab << endl;
cout << abc << endl;
cout << abcd << endl;
}
will compile using GCC and give a similar warning. Its output is different
from that of C:
1
4
4
4
a
24930
6382179
1633837924
So one-byte character literals are treated as char, while multi-byte literals
are treated as int.
<强度 > 说明 强度 >
I ran my tests on a 32-bit Linux system on which an int has 4 bytes. It would be
interesting to see what happens on other systems, e.g. on a 64-bit system.
<强 > EDIT 强 >
Fixed answer (thanks for the hint): character literals have type int in C, they
are not cast to int.