char* s =(char*) "sss";
int j = -1;
printf("%d", j < strlen(s));
It's mainly about the difference between j < 3 and j < strlen(s).
In the above code, the printf() function prints 0, but if I change strlen(s) to 3, the result becomes 1 (just as what I expect).
What is the cause?
The return type of
strlenissize_t, which is an unsigned integer type. When comparing asize_tto anint, the "usual arithmetic conversions" are applied. In this case, that means theintoperand, i.e.-1, is converted to asize_t, resulting in the largest possiblesize_tvalue, which is greater than thestrlenvalue.