nothing to add to TP as answers already provided... but very interesting discussions and references made to C standard...
as the Topic Starter is asking C++, allow me to quote the C++11 language specifications § 2.14.3 paragraph 1:
QUOTE
A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by
one of the letters u, U, or L, as in u’y’, U’z’, or L’x’, respectively. A character literal that does not begin
with u, U, or L is an ordinary character literal, also referred to as a narrow-character literal. An ordinary
character literal that contains a single c-char has type char, with value equal to the numerical value of the
encoding of the c-char in the execution character set. An ordinary character literal that contains more than
one c-char is a multicharacter literal. A multicharacter literal has type int and implementation-defined
value.
and let me quote from the C11 language specifications § 6.4.4.4 paragraph 10:
QUOTE
An integer character constant has type int. The value of an integer character constant
containing a single character that maps to a single-byte execution character is the
numerical value of the representation of the mapped character interpreted as an integer.
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. If an integer character constant contains
a single character or escape sequence, its value is the one that results when an object with
type char whose value is that of the single character or escape sequence is converted to
type int.
it's also interesting to know that in the C++11 specifications, there is a special section that talks about C Compatibility:
QUOTE
Change: Type of character literal is changed from int to char
Rationale: This is needed for improved overloaded function argument type matching. For example:
int function( int i );
int function( char c );
function( ’x’ );
It is preferable that this call match the second version of function rather than the first.
Effect on original feature: Change to semantics of well-defined feature. ISO C programs which depend on
sizeof(’x’) == sizeof(int)
will not work the same as C ++ programs.
Difficulty of converting: Simple.
How widely used: Programs which depend upon sizeof(’x’) are probably rare.
so the following code snippets will produce slightly different output in C++ vs C:
CODE
char c = '0';
cout << "size of '0': " << sizeof('0') << endl;
cout << "size of c : " << sizeof(c) << endl;
cout << "size of char: " << sizeof(char) << endl;
output (C++):
CODE
size of '0': 1
size of c : 1
size of char: 1
equivalent code snippets in C:
CODE
char c = '0';
printf("size of '0': %u\n", sizeof('0'));
printf("size of c : %u\n", sizeof(c));
printf("size of char: %u\n", sizeof(char));
output ( C ):
CODE
size of '0': 4
size of c : 1
size of char: 1
and output using 16-bit Turbo C:
CODE
size of '0': 2
size of c : 1
size of char: 1
all these bring back some fond memories... thanks guys