ISO C90 forbids mixing declarations and code... but allows it in certain instances?
In this code snippet
vec3 a = Vec3(0, 1, 2);
vec3 b = Vec3(0, 1, 2);
vec3 abc = {0}; // Declared after a function call
there are only declarations. There are no statements. Function calls used to initialize the variables are expressions. They are not statements.
It seems this warning
warning: ISO C90 forbids mixing declarations and code
is confusing. It would be more correctly to write that
warning: ISO C90 forbids mixing declarations and statements
For example even a redundant semicolon introduces a null statement. So in general the compiler should issue a warning even for the following code snippet
vec3 a = Vec3(0, 1, 2);;
^^^^
vec3 b = Vec3(0, 1, 2);
The second function has three consecutive variable definitions with initializers — that isn't a problem.
What C90 (C89) does not allow is a declaration after a statement — within a given statement block (between {
and }
), the declarations must all precede any statements (non-declarations). A plain function call, not part of an initializer, is a statement.
That's why the GCC option for reporting on the problem is -Wdeclaration-after-statement
.
You're misunderstanding the constraint. We can have declarations with initializers; the first non-declaration statement marks the end of the declarations, and after that point we're not allowed more declarations in that scope.
Non-declaration statements can be expression-statements (as above), compound statements (such as if
or while
) or blocks.