This question already has an answer here:
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;
}
Because sizeof(int) is unsigned. So -1 is converted to a large unsigned value.
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
来源:https://stackoverflow.com/questions/24466857/why-sizeofint-is-not-greater-than-1