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
intand notcharin C.So
sizeof 'a' == sizeof (int).Note that in C++, a character literal is of type
charand sosizeof 'a' == sizeof (char).