Size of character ('a') in C/C++ Size of character ('a') in C/C++ c c

Size of character ('a') in C/C++


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.


As Paul stated, it's because 'a' is an int in C but a char in C++.

I cover that specific difference between C and C++ in something I wrote a few years ago, at: http://david.tribble.com/text/cdiffs.htm


In C the type of character literals are int and char in C++. This is in C++ required to support function overloading. See this example:

void foo(char c){    puts("char");}void foo(int i){    puts("int");}int main(){    foo('i');    return 0;}

Output:

char