0

Possible Duplicate:
Size of character ('a') in C/C++

Why does this program output 4 and not 1?

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

'z' is a character and sizeof('z') must print 1?

Community
  • 1
  • 1
  • A hint: `char ch = 'z'; printf("%d", sizeof ch);` –  May 28 '11 at 18:59
  • @tjm I think it is -- nice find. (And vote cast). –  May 28 '11 at 19:01
  • 4
    What does it say about the uselessness of the SO search facility that I know I have a high-rep answer to this, but still cannot find it! –  May 28 '11 at 19:02
  • In fact the dupe answer is by me! But I couldn't find it. –  May 28 '11 at 19:05
  • @Neil, I don't know about the SO search, but it does come up as the first result if you google "c sizeof character" – tjm May 28 '11 at 19:09
  • @tjm well for me the wikipedia article comes up first. But still - anonymous fame at last! –  May 28 '11 at 19:23
  • -1 `void main` **RAAAAAAAAARRRRRRRRRGGGGGGGGGHHHHHHHHH** – pmg May 28 '11 at 20:36

2 Answers2

8

'z' is a character literal and in C a character literal is of type int. So sizeof('z') equals sizeof(int) on your implementation.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
8

Sizeof char.

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

cnicutar
  • 178,505
  • 25
  • 365
  • 392