Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is the compiler treating character as an integer?

Tags:

c

integer

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?

like image 488
shiven Avatar asked Jan 15 '23 23:01

shiven


1 Answers

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).

like image 55
ouah Avatar answered Jan 30 '23 00:01

ouah