1
void main()
{
printf("%d\n",sizeof('1'));
}

Output : 4

void main()
{
char a='1';
printf("%d\n",sizeof(a));
}

Output : 1

Can somebody say why it is different ?

2 Answers2

5

In C, character literals are of type int.

Note that this was changed in C++, in which character literals have the obvious type char.

gsg
  • 9,167
  • 1
  • 21
  • 23
1

In C, character literals are of type int, in another word, sizeof('1') is the same as sizeof(int).

While sizeof(a) is the real sizeof(char).

Yu Hao
  • 119,891
  • 44
  • 235
  • 294