2

Given the following code:

int main(){
    int i = 0, int j = 0; 
    for(int i = 0; i < 10; i++){
        static int j = 0;
        j++;
        printf("j: %d, i: %d \n", j,i);
    }
    printf("j: %d, i: %d \n", j,i);
    return 0;
}

producing the output:

j: 1 i: 0 
j: 2 i: 1 
j: 3 i: 2 
j: 4 i: 3
j: 5 i: 4 
j: 6 i: 5 
j: 7 i: 6 
j: 8 i: 7 
j: 9 i: 8 
j: 10 i: 9 
j: 0, i: 0 

I am wondering how the scope and the access possibilites of the variables i and j, defined outside the for-loop in the main file, are changing. Compiled, linked and tested with gcc -std=c11 -o

clickMe
  • 993
  • 11
  • 33
  • 6
    Simple: Use different names for the variables. – Kerrek SB Feb 26 '16 at 19:12
  • 2
    Simple - Use meaningful names for variables – Ed Heal Feb 26 '16 at 19:13
  • Even better: Don't declare variables inside loops. This way you code will be compliant with more standards.. – Eugene Sh. Feb 26 '16 at 19:13
  • Simple - reject the incoming code back to its author and state that you will not go near it until it's rewritten with meaningful names. – Martin James Feb 26 '16 at 19:15
  • 2
    I named my twins "John" and "John". When I am holding John, how does John know I am talking to him? – Weather Vane Feb 26 '16 at 19:16
  • 1
    The variables in a C program are local to a block. Thus, the variables inside the block will be given higher priority to the ones outside the block. As long as you have the names of the variables (in and outside the block) same, you wont be able to access the one's outside the block. – Abhishek Choubey Feb 26 '16 at 19:32
  • In a TLA, DDT. (TLA = "Three Letter Acronym"; DDT = "Don't Do That") – Ken Clement Feb 26 '16 at 20:34

2 Answers2

3

In your code you have defined multiple instances of both i and j (each instance occupying its own memory space). At the very least this results in difficult to understand and unmaintainable code:

 int i = 0, int j = 0; //i & j defined here
    for(int i = 0; i < 10; i++){//and new i here
        static int j = 0;//new j defined here with new block scope.

About scope: This code snippet is meaningless except to illustrate that each occurrence of i is a separate variable because of block scope, each having its own memory location: (where block scope is created using brackets {...})

int main(void) {

    int i = 2; //will remain == to 2
    if(1)
    {//new scope, variables created here with same name are different
        int i = 5;//will remain == 5
        for(int i = 8;;)
        {//new scope again, variable with same name is not the same
            int i = 0;
        }
        i = 20;// which i is it that we just changed here?
    }
    i = 30;// or here?

    return 0;
}

The take away is not to do it. Use unique and descriptive names with proper scope to avoid this ambiguity.

...how to access the variables i and j defined outside the for-loop in the main file...

Example 1: If variables are declared with global scope (eg. outside of a function in a .c file) then they are accessible everywhere in the file:

File.c

...
int gI=0, gJ=0; //defined with file global scope outside of a function
void another_func(void); 
...
int main(){

    for(gI = 0; gI < 10; gI++){

        gJ++;
        printf("gJ: %d, gI: %d \n", gJ,gI);
    }
    printf("gJ: %d, gI: %d \n", gJ,gI);//values printed here...
    another_func();
    return 0;
}
void another_func(void)
{
    printf( "gI = %d\ngJ = %d\n", gI, gJ);//...are the same here
}

Example 2: Alternately, you can declare variables with extern scope in a header file, where they can be accessible in any file that includes that header file:

file.h

...
extern int gI; //declared here, with extern scope (can see and use in another file)
extern int gJ; //ditto
void another_func(void);//prototype callable from multiple .c files
...

File.c

#include "file.h"
...
int gI=0; // initialize extern (only one time) before using anywhere.
int gJ=0; // The values of these variables are visible in any .c file
          // that #includes the header that they were created in.
...
int main(){

    for(gI = 0; gI < 10; gI++){

        gJ++;
        printf("gJ: %d, gI: %d \n", gJ,gI);
    }
    printf("gJ: %d, gI: %d \n", gJ,gI);//values printed here...
    another_func();//...are the same here
    return 0;
}

File2.c

#include "file.h"
...
void another_func(void)
{
    printf( "gI = %d\ngJ = %d\n", gI, gJ);//extern scope variables accessible here
}
Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

Put simply, you can't.

When you define a variable at a particular scope with the same name as a variable at a higher scope, the variable at the higher scope will be masked. There's no way to access the higher scope variable until the lower scope one goes out of scope.

You need to give these variables different names in order to be able to access all of them.

dbush
  • 205,898
  • 23
  • 218
  • 273