Explain why output is “-1 is greater than 4”?
CODE:
#include<stdio.h>
int main()
{
printf("%d",sizeof(int));
if(sizeof(int)>-1)
{
printf("\n\n 4 is greater than -1");
}
else
{
printf("\n\n -1 is greater than 4");
}
return 0;
}
OUTPUT:
4
-1 is greater than 4
Can you explain the above code and tell us why is it printing the output as -1 is greater than 4?
sizeof returns an unsigned int, and when comparing it with -1, it becomes MAX_INT and 4 is not greater than MAX_INT
exactly.