I tried to compare with strlen(string) with -1 but different methods gave different results:
char string[] = {"1234"};
int len = strlen(string);
int bool;
bool = -1 < strlen(string);
printf("%d",bool); //bool=0
bool = -1 < len;
printf("%d",bool); //bool=1
Assigning values to len and then comparing them gives the correct result, but I don't understand why directly comparing with strlen doesn't work.
The function
strlenhas the unsigned return typesize_tUsually the type
size_tis an alias for the typeunsigned long.In any case the rank of the unsigned type
size_tis not less than the rank of the signed typeint. So due to the usual arithmetic conversion an operand of the signed typeintis converted to the typesize_tand if the operand of the typeinthas a negative value than due to propagating the sign bit it becomes a big unsigned value.So in this expression
of the type
size_t(the common type of the expression) the left operand after conversion to the typesize_tbecomes greater than the right operand.Consider the following demonstration program.
Its output might look like