strlen
returns size_t
which is an unsigned type. You compare this to value
which is a signed type. In C, values have to be of the same type to be compared, and the rule here is that value
will be converted to size_t
before the comparison.
In your code this is no problem (except for the case I mention later about INT_MAX), there are other situations where signed-unsigned comparisons do contain bugs. See here for example .
Because of this possibility, some compilers warn about all signed-unsigned comparisons, so that you can manually check your code for bugs.
To get rid of this warning I'd suggest changing value
to have type size_t
. If you go on to use value
after the end of this code snippet then make sure that you weren't relying on any more signed-unsigned comparisons afterwards!
Ideally, scope it to the loop:
for ( size_t value = 0; ......
NB. If the string is longer than INT_MAX
then your original code causes undefined behaviour when value
is incremented beyond that; making value
be size_t
also fixes that problem.