char Constants

C treats char constants as type int, and C++ treats them as type char. For instance, consider this statement:

char ch = 'A';

In C, the constant 'A' is stored in an int-sized chunk of memory; more precisely, the character code is stored in the int. The same numeric value is also stored in the variable ch, but here it occupies just one byte of memory.

C++, on the other hand, uses one byte for 'A' as well as for ch. This distinction doesn't affect any of the examples in this text. However, some C programs do make use of char constants being type int by using character notation to represent integer values. For instance, if a system has a 4-byte int, you can do this in C:

 int x = 'ABCD'; /* ok in C for 4-byte int but not for C++ ...

Get C Primer Plus®, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.