GNU89, mixed declarations and loop initial declara

2019-01-26 19:43发布

问题:

The default C dialect for GCC and ICC is GNU89. GNU89 allows mixed declarations e.g.

int i;
i = 0;
int j;

I inferred (incorrectly) from a number of other posts on SO e.g C: for loop int initial declaration, that this meant I could do

for(int i=0; i<n; i++)

with GNU89 but when I do this I get

error: 'for' loop initial declarations are only allowed in C99 mode

Apparently, mixed declarations and loop initial declarations are not the same thing (i.e. one does not imply the other).

If I could only have one I would rather have loop initial declarations. Of course, I could just use GNU99 but that's not the point. The default is GNU89 and it already breaks some C89 rules (it also allows BCPL/C++ style comments). Is there some fundamental reason why mixed declarations are allowed but not loop initial declarations?

回答1:

Mixed declarations and statements predate C89 in other languages (e.g., Algol 68) and was a common extension among a few C89 compilers (not MSCV).

Counter variable declaration in a for statement on the other hand came in C through C++98 and to my knowledge no C89 compiler found it useful enough to add it as a C89 extension.



标签: c gcc c99 c89