2

I saw in this post that we can uncover a global variable where a local variable of the same name is in scope by using extern and a compound statement.

In that post, the example given is like

#include<stdio.h>
int a = 12;             
int main(void)          
{           
    int a = 15;             
    printf("Inside a's main local a = : %d\n", a);

    {
        extern int a;
        printf("In a global a = %d\n", a);
    }

    return 0; 
}

and the shadowed variable is a global variable.

But when I tried a program like

#include<stdio.h>
int main()
{
    int i=5;
    for(int i=0; i<2; ++i)
    {
        {
            extern int i;
            printf("\nGlobal: %d", i);
        }
        printf("\nLocal: %d", i);
    }
}

it failed.

I tried making the outer i static in case it works only if the outer variable is static but still it didn't work.

Can somebody tell me why the second program doesn't work? And how to get around it if it's possible?

Maybe this method works only if the outer variable is global?

Edit: The comments made me realize that the use of extern will just make a global variable visible. But is there a way around this without changing variable name?

J...S
  • 5,079
  • 1
  • 20
  • 35
  • It's not the same: `int i=5;` is local scope and you can't make it `extern`. If you make it `static` it is still at local scope. – Weather Vane Mar 23 '19 at 11:05
  • 1
    `extern` doesn't go "one level up". I goes to the global scope, so your `i` must be declared at the gloal scope. Your compiler should have complained. – Paul Ogilvie Mar 23 '19 at 11:08
  • @PaulOgilvie Oh, I see. So there's no way around this if it was a non-global variable? – J...S Mar 23 '19 at 11:29
  • @WeatherVane So `extern` just makes the global variables visible. But can I somehow refer to the outer `i` here? – J...S Mar 23 '19 at 11:31
  • 3
    Don't. Just use unique varaible names. – Weather Vane Mar 23 '19 at 11:34
  • @WeatherVane I'm just experimenting. Do you know if this would be possible with C++? – J...S Mar 23 '19 at 11:48
  • the posted code results in the following output from the compiler: "4:9: warning: unused variable ‘i’ [-Wunused-variable]" When compiling, always enable the warnings, then fix those warnings ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same thing – user3629249 Mar 23 '19 at 19:46
  • 1
    Due to the compiler seeing the first instance of 'i' as unused, it is eliminated in the resulting object file. So does not exist when the linker is invoked. – user3629249 Mar 23 '19 at 19:49
  • @user3629249 So there is no way to use the outer variable in such a case. Is there a way to examine the object file to see the absence of the variable? – J...S Mar 26 '19 at 06:46

1 Answers1

0

In the below code snapshot

#include<stdio.h>
int main()
{
    int i=5;
    for(int i=0; i<2; ++i)
    {
        {
            extern int i; /* just declaration */
            printf("\nGlobal: %d", i);
        }
        printf("\nLocal: %d", i);
    }
}

It gets failed because linker is unable to find the definition of i.

One way to overcome this is

#include<stdio.h>
int main()
{
    int i=5; /* linker will not search this for definition of i */
    for(int i=0; i<2; ++i)
    {
        {
            extern int i; /* search definition other than this translation unit */
            printf("\nGlobal: %d", i);
        }
        printf("\nLocal: %d", i);
    }
}
int i = 100; /* this is the definition of extern i */

Other way is to provide definition of extern variable i in the different file. Ideally extern is meant for external linkage i.e in other file, not in the same file.

Achal
  • 11,821
  • 2
  • 15
  • 37