3 ms·
Character literal is not a character in C
- gregjor 13y agoNo mystery. C's char type is a single byte, an integer type of (usually) 8 bits, though the actual size is platform-dependent. A char may be signed or unsigned, like the other integer types (short, int, long). The value of a char is the ASCII code for the character, so 'a' is stored as the integer 97. This is documented in "The C Programming Language" on pages 35-36. C does not have a string type. By convention a string is an array of char with a null (0) byte at the end. C++ follows the same conventions for char and arrays of char, but implements "real" strings with more string-like behavior.
- adamnemecek 13y agoThis is one of the few examples of code that is valid in both C and C++ but that produces different results (in C++ the size would be 1).
- J_Darnley 13y agoAll integer constants "start" as type int. The message isn't about what size your value is but the fact that you are comparing a pointer with an integer. Cast to a char and you still get the same message
- kahirsch 13y agoIn fact, you can have multi-character character constants, e.g. printf("sizeof('abcd') %d\n", sizeof('abcd')); printf("'abcd' = %d\n", 'abcd'); produces sizeof('abcd') 4 'abcd' = 1633837924