3

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?

Community
  • 1
  • 1
Z boson
  • 32,619
  • 11
  • 123
  • 226
  • 2
    The reasons are mainly historical - just pick whatever standard you want to work with and which doesn't break your code base. – Paul R Apr 22 '14 at 20:54
  • @PaulR, I don't have control over which standard OPs pick on SO. The largest source of errors with C and OpenMP is due to race conditions on inner loops where the loop iterate was not made private (there were two in one day last week). It's even more common than misaligned loads with SSE. These errors would go away if loop initial declarations were allowed with the most common C standard (GNU89). – Z boson Apr 23 '14 at 07:01
  • OK, well I think you're out of luck. As far as default compiler options go, if I could wave a magic wand and change one thing it would be making `-Wall` the default for gcc - that would probably halve the number of noob C/C++ questions on SO overnight. ;-) – Paul R Apr 23 '14 at 12:55
  • 1
    Duly noted @PaulR, I'll use `-Wall` henceforth :-) – Z boson Apr 24 '14 at 09:07

1 Answers1

6

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.

ouah
  • 142,963
  • 15
  • 272
  • 331