Reputation: 237
I'm unable to understand the following for loop. Please help me. Thanks for your time. I'm unable to understand how the for loop becomes true in the first iteration. if s[0] = "d", then how "d" will be treated like "true".
#include <stdio.h>
int main()
{
char s[] = "d";
int i;
for(i = 0; s[i]; i++)
printf("%c %c %c %c",s[ i ], *(s+i), *(i+s), i[s]);
return 0;
}
output : dddd
Upvotes: 2
Views: 124
Reputation: 311088
According to the C Standard (6.5.2.1 Array subscripting)
2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently,a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).
So s[i]
is equivalent to *( s + i )
that from the mathematical point of view is equivalent to *( i + s )
and in C may be rewritten like i[s]
because in any case the expression is considered like *( i + s )
.
Thus all these constructions
s[ i ]
*(s+i)
*(i+s)
i[s]
are equivalent in C and denote the i-th
element of the array s
.
As for your comment to my answer
Could you please explain how the for loop becomes true during the initial iteration?
then the array s
is initialized by the string literal "d"
.
char s[ ] = "d";
A string literal includes into itself the terminating zero character '\0'
. So the declaration above can be in fact equivalently rewritten the following way
char s[ ] = { 'd', '\0' };
As the first element of the array is not equal to zero (it is equal to the character 'd'
) then the condition s[i]
that is equivalent to the condition s[i] != 0
is true when i
is equal to 0
.
And a quote regarding to the for-statement (6.8.5 Iteration statements)
4 An iteration statement causes a statement called the loop body to be executed repeatedly until the controlling expression compares equal to 0. The repetition occurs regardless of whether the loop body is entered from the iteration statement or by a jump.1
Upvotes: 1
Reputation: 2652
char s[] = "d"
initializes s
to an array containing the character 'd'
followed by a null terminator ('\0'
).
In the initial iteration of the for loop, we check s[0]
to determine whether to run the loop. This checks the first character of s
, which is 'd'
. In C, any character besides '\0'
is considered true, and '\0'
is considered false. (This is because C treats characters similar to integers, where 0 is false and any other int is true.)
So the for loop initially checks 'd'
, which is a true value, and runs the loop.
On the second iteration, it looks at s[1]
, which is '\0'
. This is a false value, so it doesn't run the loop a second time.
Upvotes: 1
Reputation: 11020
char s[] = "d";
declares a string literal, which is zero-terminated. In your for loop the break condition is s[i]
which means it will run until it gets to the NULL character (which will be the second character).
Upvotes: 1