Alternative for-loop syntax [duplicate]
Solution 1:
Unfortunately, this is not easy to read. You are misreading the second case of the for
statement. The first semicolon is an integral part of declaration
and thus hidden to your eyes. You can easily check such syntax questions by looking into Annex A. There you have:
(6.7) declaration:
declaration-specifiers init-declarator-listopt ;
static_assert-declaration
Solution 2:
If you see, the syntax is,
for ( declaration expression1opt ; expression2opt ) statement
Let's compare it with a general statement
for (int i = 0; i < 10; i++) printf("%d \t", i);
Here,
-
int i = 0;
denotesdeclaration
[includes the;
] -
i < 10
denotesexpression1opt
[optional] -
;
is as per the syntax requirement of;
[must, as described in syntax] -
i++
is theexpression2opt
[optional] -
printf("%d \t", i);
is thestatement
Now, in your case,
for (int i = 0, i; i++) { /* ... */ }
-
int i = 0, i;
denotesdeclaration
-
i++
denotesexpression1opt
-
;
is missing .....
The last point here produces the error. You need to have the ;
to pass the syntax check.