I noticed that the scope rules of the for
loop are different for C and C++.
For example, the code below is legal in the C compiler, but not legal in the C++ compiler.
for (int i = 0; i < 10; ++i) {
int i = 5;
}
The above code is valid in C, but gives a redefinition error in C++.
My guess is that the C compiler treats the loop as if there is another scope inside the loop like below.
for (int i = 0; i < 10; ++i) {
{
int i = 5;
}
}
Why does the C compiler allow a second variable with the same name to be defined in the scope of the loop? Is there any particular reason or advantage for doing this?
In C/C++, the scope of a variable declared in a for or while loop (or any other bracketed block, for that matter) is from the open bracket to the close bracket.
Is it okay to create multiple for loops with the same variable name? Yes, it's OK and widespread practice to do so.
There is no such thing as "construction" in C, so the space for the variable will simply be allocated into the stack (without any initialization), when the function is called. That's why there is a "zero" cost when declaring the variable inside a loop.
C - Scope Rules. A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language −. Inside a function or a block which is called local variables.
Scope rules in C or scope of a variable means that from where the variable may directly be accessible after its declaration. Local variable: Local variables are the variables that are declared inside a block or a function. These variables can only be used inside that block or function.
In the code above, the global variable i is different from one which is controlling the for loop. when while loop is executed - the variable i defined inside while is having local scope, where as the variable under (i > 3) follows the global variable, and doesn't refer to local scope. Dipan.
The scope of variables created in any compound statement is limited to the compound statement itself. So this really isn't a special rule for loops. Loops and selection statements do have their own rules for the variables created as a part of the loop or selection statement itself.
As far as standards go, for
loop scopes are indeed defined differently in C and in C++. In C they are defined as follows:
6.8.5.3 The for statement
The statement
for ( clause-1 ; expression-2 ; expression-3 ) statement
behaves as follows: ...
with no specific reference given to limitations on variable declarations inside the statement
. The top-level description of loops ("iteration statements" in the standard) specifies:
An iteration statement is a block whose scope is a strict subset of the scope of its enclosing block. The loop body is also a block whose scope is a strict subset of the scope of the iteration statement.
as you've hinted in your question, code like:
for (int i = 0; i < 10; ++i)
int i = 5;
where a new block is not declared (notice the missing { }
surrounding the redeclaration) - is not valid code.
Whereas in C++, there is a specific reference to redeclaration of loop variables:
8.6 Iteration statements specify looping ... If a name introduced in an init-statement or for-range-declaration is redeclared in the outermost block of the substatement, the program is ill-formed.
As it relates to rationale - likely C++ just added the restriction as an improvement to language semantics, indeed redeclaring a variable this way is usually a bug.
Arguably, this restriction is appropriate in C as well - but adding this restriction to the C standard would break backwards compatibility with existing code and is unlikely to happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With