How can a char be used as a boolean?

I've seen code like this to convert all chars in a string to uppercase:

1
2
for (int i=0; str[i]; i++)
        str[i] = toupper(str[i]);


I get it works to turn a string to uppercase, but I don't understand the condition in the for instruction: str[i].
How does that read? If str[i] is a char, how can it be interpreted as a boolean condition?
Thanks.
Two things. First, a character string is terminated by a null value (a byte with the value zero). Second, when testing any expression as a boolean value, zero represents false and any non-zero value represents true.

Thus the code as in the above example is simply saying keep processing until the end of the string.

Character string tutorial:
http://www.cplusplus.com/doc/tutorial/ntcs/
Ahh! I had forgotten about the null value at the end of the string.
Thanks, Chervil!!
Topic archived. No new replies allowed.