user11954200
user11954200

Reputation:

Defining a variable before or within a loop

Is there a difference between the following two approaches to defining a for-loop variable in C?

int i;
for (i = 0; i < X; i++) {
    // something
}

And:

for (int i = 0; i < X; i++) {
    // something
}

My preference is to use the second approach if the i is just always a throw-away, but is there any reason that it wouldn't be a good idea to do that?

Upvotes: 0

Views: 147

Answers (3)

Peter
Peter

Reputation: 36597

There are a number of differences between the two.

The second form is illegal in the 1989 ANSI (1990 ISO) C standard. The first is supported in C from 1999 and in standard C++. It is supported by some C compilers older than 1999, either as a non-standard or optional extension or because those compilers were actually C++ compilers with a C mode.

In the first form, i exists after the loop, so its value can still be accessed, but redefining it results in a diagnostic (compile time error). In the second form, i does not exist after the loop, so accessing its value after the loop gives a diagnostic, but i can be redefined.

In general terms, it is advisable to ensure that variables only exist for as long as needed, and cease to exist when no longer needed. The second form explicitly allows that.

Obviously, variables that need to exist outside the loop, need to be defined outside it. But, if the variable i is not needed outside the loop, then I would favour the second form. This allows the compilers to catch problems, such as unintended use of variable i after the loop.

Some older C and C++ compilers (mainly dating from before the 1998 C++ standard was ratified, but some from the early 2000s) implement the second form so the variable i still exists after the loop. This effectively makes the two forms equivalent when using those compilers.

Upvotes: 1

89f3a1c
89f3a1c

Reputation: 1488

Yes.

As the i variable is usually used only to count the number of iterations needed, it doesn't make sense to have a variable live outside the scope of the loop. That, if you can, should be avoided.

As some comments to the question mention, there are some cases in which you can't use the second, but that's not the general case.

As for the compiler later compiling to the same assembly, that may be true, but conceptually the second is cleaner, and for someone reading the code from outside, it makes it clear that the variable is never used again.

Hope this helps!

Upvotes: 2

Acorn
Acorn

Reputation: 26066

but is there any reason that it wouldn't be a good idea to do that?

In C, you should prefer the second form, because it reduces the scope of the variable and makes it more obvious where it is going to be used. Unless...

  • ...it goes against the coding guidelines of a given project. For instance, the Linux kernel declares all variables at the top of a function.

  • ...you want to be conforming to C90: you cannot use loop initial declarations.

In C++, however, objects may have very expensive constructors, which means that, at times, you may want to re-use them rather than initialize a new one every time (e.g. if you construct a new one within the body of the loop).

Upvotes: 1

Related Questions