12

Possible Duplicate:
Why are C character literals ints instead of chars?

#include<stdio.h>
int main(void)
{
    char b = 'c';
    printf("here size is %zu\n",sizeof('a'));
    printf("here size is %zu",sizeof(b));
}

here output is (See live demo here.)

here size is 4 
here size is 1

I am not getting why sizeof('a') is 4 ?

Community
  • 1
  • 1
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • See [Why are C character literals ints instead of chars?](http://stackoverflow.com/questions/433895/why-are-c-character-literals-ints-instead-of-chars) – Bertrand Marron Dec 28 '11 at 10:46

3 Answers3

12

Because in C character constants, such as 'a' have the type int.

There's a C FAQ about this suject:

Perhaps surprisingly, character constants in C are of type int, so sizeof('a') is sizeof(int) (though this is another area where C++ differs).

cnicutar
  • 178,505
  • 25
  • 365
  • 392
10

The following is the famous line from the famous C book - The C programming Language by Kernighan & Ritchie with respect to a character written between single quotes.

A character written between single quotes represents an integer value equal to the numerical value of the character in the machine's character set.

So sizeof('a') is equivalent to sizeof(int)

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
3

'a' by default is an integer and because of that you get size of int in your machine 4 bytes.

char is 1 bytes and because of this you get 1 bytes.

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123