Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
Scope rules of `for` loop different in C++ than in C?

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda