tags:

views:

56

answers:

2
void main()
{
    char c='0';
    printf("%d %d",sizeof(c),sizeof('0'));
}
A: 

The output will be 1 4. The type of the '0' literal is int, which on most systems has a size of 4. The C standard requires that sizeof(char) is 1.

If you get anything less than 4, get in your time machine, and dial in +25 years.

Matt Joiner
butturbo c gives output as 1,1
@user : Did you compile the program as `*.cpp` on Turbo?
Prasoon Saurav
I believe Turbo C predates the Standard.
Gabe
The output _may_ be 4, it may also be 2, or 7000. As long as the `int` can take the range -32767 thru 32767 inclusive, the standard allows any value (with the usual provisos, including that is has to be at least as big as a `short` and no bigger than a `long`, blah blah blah). I just find it annoying when someone only half-quotes the standards :-)
paxdiablo
+2  A: 

In C, size of char literal is equal to sizeof(int). So sizeof('0') gives the value of sizeof(int) on your implementation.

Also sizeof(char) is always 1 as mandated by the Standard.

Prasoon Saurav