First of all the variable s
is a null terminated C-string, which is an array of chararcters with the last character being '\0'
. You access that array with the index i
in your code.
With that for loop you loop through the s
string until a null terminator '\0'
or the char c
is found.
The null terminator '\0'
is 0 decimal which means false in boolean logic because everthing else than 0 is true.
If you write for example:
char a = 'A'; /* same as char a = 65; */
if (a) { ... }; /* same as if (a != 0) */
that means: if a is true
which is: if a is not false
and better: if a is not equal to 0
. This statement will evaluate to true because 'A'
is 65 decimal (ASCII code) which is not equal to 0.
The for loop you've asked for can be rewritten as:
for (i = 1; s[i] != '\0' && s[i] != c; i++);
I would recommend to use explicit statements like s[i] != '\0'
because it is easier to read.