Variable declaration on & ldquo; Start value & rdquo; From FOR loop - What C standards allow it?

advertisements

C Language

On which C Standard the following code compiles without error (C89, C99, C11)

for (int i = 0; i < 10; ++i) {

    DO SOMETHING...

}

I understand that some C compilers won't accept the version above and the variable "i" must be declare outside the parentheses. Like so:

int i;
for (i = 0; i < 10; ++i) {

    DO SOMETHING...

}


This is allowed since c99. So c99 and c11 support it.

In c89, the first clause of a for statement can only be an expression. In c99 and c11 it can be an expression or a declaration. Only one single declaration is allowed (though this can declare several variables).