-2

i have the following code:

int main() { 
     char x='a';
     printf("integer = %ld\ncharacter  = %ld\nx = %ld\n", sizeof(int),sizeof('a'),sizeof(x);  
     return 0;  
}

and the output is:

integer = 4
character = 4
x = 1

my question is that in why the size of the char variable x is not equal to the size of the 'a' .

what actually brings the difference?

thanks for the help

Deepu
  • 131
  • 6

1 Answers1

3

Because in C single character constants (e.g., 'a' or '0') are of type int not char. This is different in C++ where they are of type char.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    +1 especially for mentioning this as a difference from C++. I always wonder about such subtle differences, now I've got one. –  Mar 03 '13 at 21:35
  • 1
    @H2CO3 May be, they deliberately introduced such subtleties so as to rebut when people say "C/C++" ;) – P.P Mar 03 '13 at 21:42
  • @KingsIndian Correct! –  Mar 03 '13 at 21:43