Type difference of character literals in C and C++

Every literal (constant) in C/C++ will have a type information associated with it.

In both C and C++, numeric literals (e.g. 10) will have int as their type. It means sizeof(10) and sizeof(int) will return same value.

However, character literals (e.g. ‘V’) will have different types, sizeof(‘V’) returns different values in C and C++. In C, a character literal is treated as int type where as in C++, a character literal is treated as char type (sizeof(‘V’) and sizeof(char) are same in C++ but not in C).

filter_none

edit
close

play_arrow

link
brightness_4
code

int main()
{
   printf("sizeof('V') = %d sizeof(char) = %d", sizeof('V'), sizeof(char));
   return 0;
}

chevron_right


Result of above program:

C result – sizeof(‘V’) = 4 sizeof(char) = 1



C++ result – sizeof(‘V’) = 1 sizeof(char) = 1

Such behaviour is required in C++ to support function overloading. An example will make it more clear. Predict the output of the following C++ program.

filter_none

edit
close

play_arrow

link
brightness_4
code

void foo(char c)
{
   printf("From foo: char");
}
void foo(int i)
{
   printf("From foo: int");
}
  
int main()
{
   foo('V');
   return 0;
}

chevron_right


The compiler must call

filter_none

edit
close

play_arrow

link
brightness_4
code

void foo(char);

chevron_right


since ‘V’ type is char.

Article contribution by Venki and Geeksforgeeks team.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.