I have a small snippet of code. When I run this on my DevC++ gnu compiler it shows the following output:
main ()
{ char b = 'a';
printf ("%d,", sizeof ('a'));
printf ("%d", sizeof (b));
getch ();
}
OUTPUT: 4,1
Why is 'a'
treated as an integer, whereas as b
is treated as only a character constant?
Because character literals are of type int
and not char
in C.
So sizeof 'a' == sizeof (int)
.
Note that in C++, a character literal is of type char
and so sizeof 'a' == sizeof (char)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With