GNU89, mixed declarations and loop initial declarations

孤人 提交于 2019-12-18 07:04:33

问题


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.



来源:https://stackoverflow.com/questions/23229872/gnu89-mixed-declarations-and-loop-initial-declarations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!