Uncovering shadowed variable when outer variable is not globalHow to print value of global variable and local variable having same name?Where are static variables stored in C and C++?When to use self over $this?Using global variables in a functionWhat is the scope of variables in JavaScript?How to declare global variables in Android?How do I use extern to share variables between source files?Is there a reason for C#'s reuse of the variable in a foreach?Global variables in AngularJSRe-defining global variable with conflicting local variableWhat is the Static Global Variable Purpose?

Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?

Cropping a message using array splits

On what legal basis did the UK remove the 'European Union' from its passport?

Was there ever any real use for a 6800-based Apple I?

How do I compare the result of "1d20+x, with advantage" to "1d20+y, without advantage", assuming x < y?

How could we transfer large amounts of energy sourced in space to Earth?

Is there enough time to Planar Bind a creature conjured by a 1-hour-duration spell?

Is it a bad idea to replace pull-up resistors with hard pull-ups?

The lexical root of the perfect tense forms differs from the lexical root of the infinitive form

How can a Lich look like a human without magic?

Why does TypeScript pack a Class in an IIFE?

find not returning expected files

Are there variations of the regular runtimes of the Big-O-Notation?

How can I answer high-school writing prompts without sounding weird and fake?

Can I use my laptop, which says 240V, in the USA?

Control variables and other independent variables

Limit of an integral vs Limit of the integrand

What does it mean with the ask price is below the last price?

Is a vertical stabiliser needed for straight line flight in a glider?

histogram using edges

A musical commute to work

Is a diamond sword feasible?

We are two immediate neighbors who forged our own powers to form concatenated relationship. Who are we?

What is the best way for a skeleton to impersonate human without using magic?



Uncovering shadowed variable when outer variable is not global


How to print value of global variable and local variable having same name?Where are static variables stored in C and C++?When to use self over $this?Using global variables in a functionWhat is the scope of variables in JavaScript?How to declare global variables in Android?How do I use extern to share variables between source files?Is there a reason for C#'s reuse of the variable in a foreach?Global variables in AngularJSRe-defining global variable with conflicting local variableWhat is the Static Global Variable Purpose?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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 = : %dn", a);


extern int a;
printf("In a global a = %dn", 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?










share|improve this question
























  • 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 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 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 at 11:29






  • 3





    Don't. Just use unique varaible names.

    – Weather Vane
    Mar 23 at 11:34






  • 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 at 19:49

















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 = : %dn", a);


extern int a;
printf("In a global a = %dn", 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?










share|improve this question
























  • 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 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 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 at 11:29






  • 3





    Don't. Just use unique varaible names.

    – Weather Vane
    Mar 23 at 11:34






  • 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 at 19:49













2












2








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 = : %dn", a);


extern int a;
printf("In a global a = %dn", 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?










share|improve this question
















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 = : %dn", a);


extern int a;
printf("In a global a = %dn", 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?







c scope global-variables local-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 11:34







J...S

















asked Mar 23 at 10:32









J...SJ...S

3,64911231




3,64911231












  • 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 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 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 at 11:29






  • 3





    Don't. Just use unique varaible names.

    – Weather Vane
    Mar 23 at 11:34






  • 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 at 19:49

















  • 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 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 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 at 11:29






  • 3





    Don't. Just use unique varaible names.

    – Weather Vane
    Mar 23 at 11:34






  • 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 at 19:49
















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 at 11:05






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 at 11:05





1




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 at 11:08





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 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 at 11:29





@PaulOgilvie Oh, I see. So there's no way around this if it was a non-global variable?

– J...S
Mar 23 at 11:29




3




3





Don't. Just use unique varaible names.

– Weather Vane
Mar 23 at 11:34





Don't. Just use unique varaible names.

– Weather Vane
Mar 23 at 11:34




1




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 at 19:49





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 at 19:49












1 Answer
1






active

oldest

votes


















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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312797%2funcovering-shadowed-variable-when-outer-variable-is-not-global%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer



























      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.






      share|improve this answer

























        0












        0








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 12:18









        AchalAchal

        10.1k2931




        10.1k2931





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312797%2funcovering-shadowed-variable-when-outer-variable-is-not-global%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript