0

I am not able to understand why this piece of code is giving output False:

   if (sizeof(int) > -1)
       printf("True");
   else
       printf("False");

As I tried to print what sizeof(int) is returning is 4.

dragosht
  • 3,237
  • 2
  • 23
  • 32
rscoder1
  • 1
  • 1

2 Answers2

3

The result of the sizeof operator has type size_t. Your -1 is a signed int. When the two are compared, the latter is converted to size_t, which results in a rather large unsigned value.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
1

By standard sizeof returns an unsigned integer type size_t. Although the exact type is implementation defined it is certain to be unsigned. When you try to compare it to the signed integer -1, -1 gets converted to max value of this type(try writing (unsigned)-1 and examine the value) and thus the comparison is false.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176