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.
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.
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.
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.