1

Recently, I accidentally wrote C code that looks like this:

for (size_t i = 0; i < SOME_VALUE; ++i)
{
    for (size_t i = 0; i < ANOTHER_VALUE; ++i)
    {
        // do work with 'i' from inner loop *WITHOUT* any disruption to 'i' from outer loop
    }
}

Reading the code closely, I discovered this "bug". However, at runtime, it did not cause any issues. To be clear, my code was compiled with a relatively recent version of MinGW-w64 to create a native Win32 binary.

Sure, the code works, but I am surprised this is allowed, and even more surprised that my GCC-ish compiler did not complain about it! (Is there a GCC warning option for this mistake?)

Can someone explain why this is allowed, and why the inner and outer i variables do not conflict / disrupt? Ideally, someone can point me to official ISO-ish C language definition that allows (and supports) this behaviour!

Inspired by: What versions of C allow you to declare variables in for loops?

5
  • 1
    Called shadowing. Related: stackoverflow.com/questions/32254127/… Commented Mar 7, 2022 at 17:13
  • 1
    This is just variable shadowing Commented Mar 7, 2022 at 17:13
  • 1
    Variables in the inner block will simply shadow outer ones with the same name. This has always been allowed in C. Commented Mar 7, 2022 at 17:13
  • 1
    Shadowing variables in inner scopes is absolutely standard and normal (though often discouraged for the reasons you've found). Commented Mar 7, 2022 at 17:13
  • Possible duplicate: stackoverflow.com/q/25151524/695132 Commented Mar 7, 2022 at 17:14

1 Answer 1

2

This is allowed, and is commonly referred to as shadowing, where the variable at the innermost scope shadows any variable with the same name at an outer scope.

This is explicitly allowed as specified in section 6.2.1p4 of the C standard regarding Scope of Identifiers:

... If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

Sign up to request clarification or add additional context in comments.

3 Comments

It is called “hiding” in the C standard, not “shadowing”. There is no darkness (blocking of light) cast upon previous identifiers. Rather, they are not visible, hence hidden.
shadow in common usage means the opposite of what you're saying in the variable at the innermost scope shadows any variable with the same name. It's typically used to mean a person or thing in another's shadow, not the one casting a shadow, e.g.: the new guy is going to shadow you for the next few days to get up to speed.
The name of the gcc warning is -Wshadow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.