Why does this code produces different o/p in C and C++?
int i = sizeof('a');
In C, the type of a character constant like 'a'
is actually an int
, with size of 4 (or some other implementation-dependent value). In C++, the type is char
, with size of 1. This is one of many small differences between the two languages.
Character literals in C are int
s, whereas in C++ they are char
s. In any case sizeof(char)
is always 1 by definition.