0
#include<stdio.h>
void main()
{
int a=2;
if ((sizeof(a))>-1)
printf("a");
else
printf("b");
}

Why is the program giving output as b

When sizeof(a) = 4, which is greater than (-1)

1 Answers1

4

sizeof returns size_t (which is implementation defined unsigned integer type).

So -1 gets converted to unsigned too. Assuming two's complement representation of negative integers, (unsigned)-1 is greater than (unsigned)4, hence the output is b.

AlexD
  • 32,156
  • 3
  • 71
  • 65
  • Nitpicking: Depending on the implementation `sizeof` might lead to `unsigned long`. However the issue here is the `unsigned` vs. `signed`. – alk Sep 22 '14 at 15:20
  • @alk Yep, thanks! It is `size_t` in fact. Corrected. – AlexD Sep 22 '14 at 15:24