#include <stdio.h>
int main()
{
char ch[100]="A dog is running out!";
int i;
for(i=0; i<=20; i++){
printf("%c",ch[i]);
}
printf("\n");
//What is the situation of for()-loop ?
for(i=0; ch[i]; i++){ //Does an array return a value? //What is that value?
printf("%c",ch[i]);
}
printf("\n");
return 0;
}
C-strings (char arrays) end with a null character (literal value of zero). Since strings can be of any length, this is how the computer knows it has reached the end of the string.
1 2 3
for( ...; ch[i]; ... ) // <- this loop condition
for( ...; ch[i] != 0; ... ) // <- is a shorthand way to say this
The null is put there automatically by the "double quotes":
1 2 3 4 5
char foo[20] = "foo"; // this
// is the same as this:
char foo[20] = { 'f', 'o', 'o', 0 }; // <- note it ends with a literal 0
// that marks the end of the string
The 2nd loop is simply looking for that end-of-string marker.