void main()
{
char c='0';
printf("%d %d",sizeof(c),sizeof('0'));
}
views:
56answers:
2
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
2010-09-29 05:51:54
butturbo c gives output as 1,1
2010-09-29 05:53:51
@user : Did you compile the program as `*.cpp` on Turbo?
Prasoon Saurav
2010-09-29 05:55:56
I believe Turbo C predates the Standard.
Gabe
2010-09-29 05:56:53
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
2010-09-29 06:08:28
+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
2010-09-29 05:53:43