3

The following program:

#include <stdio.h>
int main()
{
    char i='u';
    for (int i=0;i<=3;i++)
    {
        printf("%d\n",i);
    }
    return 0;
}

prints 0,1,2,3 in new lines, since the character variable i declared outside the for loop is "hid". But, the following program:

#include <stdio.h>
int main()
{
    char i='u';
    for (int i=0;i<=3;i++)
    {
        int i=8;
        printf("%d\n",i);
    }
    return 0;
}

print 8 '4' times on new lines as if the variable i initialized as 8 has more priority than the variable i (counter) going from 0 to 3 .

The i in the for initialization and the i in the for loop are in the same block, but one seems to have more priority than the other. Does such a priority really exist and if yes, is there a defined order of priority?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • such priority is defined by [scoping rules](https://en.cppreference.com/w/c/language/scope), that is if variable of the same name is declared in outer scope, the visibility of the outer scope variable ends at the point of declaration of the inner variable – dvhh Apr 23 '20 at 17:45
  • *"The `i` in the for initialization and the `i` in the for loop are in the same block"*. That's not true. From the C standard: *"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."* – user3386109 Apr 23 '20 at 18:01

2 Answers2

1

What you're calling "priority" is actually referred to as scope.

Each variable and function has an enclosing scope in which the name is valid and in which it has a particular lifetime. If a variable in one scope has the same name as a variable at a higher scope, the variable(s) at the outer scope(s) is masked and only the one in the innermost scope is visible.

In the case of a variable declared in the initialization section of a for loop, these variables have a scope that is visible in the other parts of the for as well as the loop body. If the loop body is a compound statement, that starts another scope, and variables declared here will mask variables with the same name at higher scopes.

So in your second program you have three variables named i:

  • The scope of the first i is the body of the main function.
  • The scope of the second i is for statement.
  • The scope of the third i is the body of the for statement.
dbush
  • 205,898
  • 23
  • 218
  • 273
0

Such priority is defined by scoping rules:

The scope of any other identifier begins just after the end of its declarator and before the initializer, if any:

int x = 2; // scope of the first 'x' begins
{
    int x[x]; // scope of the newly declared x begins after the declarator (x[x]).
              // Within the declarator, the outer 'x' is still in scope.
              // This declares a VLA array of 2 int.
}
unsigned char x = 32; // scope of the outer 'x' begins
{
    unsigned char x = x;
            // scope of the inner 'x' begins before the initializer (= x)
            // this does not initialize the inner 'x' with the value 32, 
            // this initializes the inner 'x' with its own, indeterminate, value
}
 unsigned long factorial(unsigned long n)
// declarator ends, 'factorial' is in scope from this point
{
   return n<2 ? 1 : n*factorial(n-1); // recursive call
}
dvhh
  • 4,724
  • 27
  • 33