3

this is my C code : why is the output "False " ?????

why 4 > -1???

code :

#include <stdio.h>

int main() {
    if (sizeof(int) > -1)
        printf("True");
    else
        printf("False");
    return 0;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Never Back Down
  • 138
  • 2
  • 12

2 Answers2

11

Because sizeof(int) is unsigned. So -1 is converted to a large unsigned value.

MTilsted
  • 5,425
  • 9
  • 44
  • 76
4

Because sizeof yields a value of type size_t which is an unsigned type. In > expression usual arithmetic conversions will convert -1 to an unsigned type which is the type of the > result. The resulting value will be a huge positive value.

To get the expected behavior use:

(int) sizeof (int) > -1
ouah
  • 142,963
  • 15
  • 272
  • 331