Possible Duplicate:
Size of character ('a') in C/C++
I am a beginner at C, and was confused by this.
C: I tried printing the sizeof( ‘a’ ) in C using the “%zu” modifier, and it prints a value 4.
C++: Printing sizeof( ‘a’ ) in C++ using cout, and printf(using the format above) both printed a value 1.
I believe the correct value should be 1, since ‘a’ will be taken as a char. Why doesn’t it return 4 in C? Are the sizeof operations of both different in both the languages? If so, what’s the difference, and why does it return a different value?
I used the gcc compilers in both cases.
In C, the
'a'is a character constant, which is treated as an integer, so you get a size of 4, whereas in C++ it’s treated as achar. This is a duplicate of the question here:Size of character ('a') in C/C++