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;
}
πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
Never Back Down
  • 126
  • 2
  • 10

2 Answers2

11

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

MTilsted
  • 5,038
  • 9
  • 40
  • 71
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
  • 138,975
  • 15
  • 262
  • 325