Frage

I noticed that C11 no longer allows you to declare iterator variables within the loop construction, e.g., the following is invalid:

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

But this is OK:

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

Given the fact that before C99 it was not even possible to declare variables except at the beginning of a function (block? Does C11 allow variable declarations at any place in a function?), I'm curious how this relates to safety practices. Should iterators be declared at the top of the function or block in which they occur, or just above the first loop they are used in? I can't see as it makes much difference either way, but the former method seems more robust to moving code around. As far as safety goes, I don't see any obvious implications.

Also, what was the reason for the above change in C11 in the first place? I rather liked the syntax in the first example.

EDIT: to my last question, I suppose one issue is that if the iterator is used multiple times, moving for (int i = 0; ... around within a block would not be as easy to refactor.

War es hilfreich?

Lösung

Where did you get this idea?

The C11 draft, section 6.8.5 (1), says:

iteration-statement:

while ( expression ) statement

do statement while ( expression ) ;

for ( expressionopt ; expressionopt ; expressionopt ) statement

for ( declaration expressionopt ; expressionopt ) statement

That last form makes it pretty clear that a declaration is still allowed in the first clause of the for statement.

[Update]

Note that declaration is something like int i = 0 ;. That is, it includes the semi-colon (see section 6.7). So for (int i = 0 ; i < 10 ; ++i) is definitely allowed by C11.

[Update 2]

As for when to use which, that is a matter of opinion. My opinion is that you should embed the declaration whenever you can, because it makes the code easier to read.

If I am reading your code and I see:

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

...then I know you are not relying on the value of i after the loop terminates, because it is no longer in scope.

If I see:

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

...then I expect you to use the value of i later; e.g. maybe you break early and want to know what i was when that happened. If I do not see you use i later, I will probably wonder what you were thinking and/or what I am missing.

As usual, readability is the most important metric. Of course, what qualifies as "readable" is a matter of opinion.

Andere Tipps

According to the question you linked, C11 should still allow you to declare variables inside the for loop (why would they they break backwards compatibility like that anyway?).

In any case, if you are compiling against C89 (the C standard supported by the Microsoft compiler), then you can only declare new variables at the start of a block (ie.: right after a curly brace). The good news is that you can put a new block anywere, not just at the top of a function so you can emulate the C99 for-loop scope by wrapping it inside an extra set of curly braces:

/* ... */
{int i; for(i=0; i<10; i++){
    /* ... */
}}
/* ... */
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top