Sizeof ('z') leads to unexpected

Possible duplicate:
Character size ('a') in C / C ++

Why does this program output 4, not 1?

void main()
{
   printf("%d",int(sizeof('z')));
}

'z' is a character, but sizeof('z')should print 1?

0
source share
2 answers

'z'is a character literal, and in C a character literal is of type int. Thus, it sizeof('z')is equal sizeof(int)to your implementation.

+8
source

Char size .

Perhaps surprisingly, character constants in C are of type int , sosizeof('a') sizeof(int)

+8
source

All Articles