Will a value live perpetually when there's no reference to it

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












Suppose the following minimal codes:



#include <stdio.h>
char character = 'c';
int main (void)

char character = 'b';
printf("The current value of head is %c", character);



I overwrote character within main,

Then what happened to c, will it be destroyed automatically or live in the memory perpetually.



The comment click me:

"variables in C are nothing but named chunks of memory. "










share|improve this question



















  • 1




    This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
    – Eli Korvigo
    1 hour ago











  • So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
    – rider dragon
    1 hour ago














up vote
6
down vote

favorite
1












Suppose the following minimal codes:



#include <stdio.h>
char character = 'c';
int main (void)

char character = 'b';
printf("The current value of head is %c", character);



I overwrote character within main,

Then what happened to c, will it be destroyed automatically or live in the memory perpetually.



The comment click me:

"variables in C are nothing but named chunks of memory. "










share|improve this question



















  • 1




    This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
    – Eli Korvigo
    1 hour ago











  • So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
    – rider dragon
    1 hour ago












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





Suppose the following minimal codes:



#include <stdio.h>
char character = 'c';
int main (void)

char character = 'b';
printf("The current value of head is %c", character);



I overwrote character within main,

Then what happened to c, will it be destroyed automatically or live in the memory perpetually.



The comment click me:

"variables in C are nothing but named chunks of memory. "










share|improve this question















Suppose the following minimal codes:



#include <stdio.h>
char character = 'c';
int main (void)

char character = 'b';
printf("The current value of head is %c", character);



I overwrote character within main,

Then what happened to c, will it be destroyed automatically or live in the memory perpetually.



The comment click me:

"variables in C are nothing but named chunks of memory. "







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago

























asked 1 hour ago









rider dragon

1916




1916







  • 1




    This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
    – Eli Korvigo
    1 hour ago











  • So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
    – rider dragon
    1 hour ago












  • 1




    This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
    – Eli Korvigo
    1 hour ago











  • So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
    – rider dragon
    1 hour ago







1




1




This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
– Eli Korvigo
1 hour ago





This is not directly relevant to your question, but I think one thing is worth pointing out. Besides cleaning up call frames, C doesn't do any memory management for you. Even if you've overwritten a global variable, its former value is not "destroyed automatically" (as an object with no references left), but is literally overwritten in-place, because variables in C are nothing but named chunks of memory.
– Eli Korvigo
1 hour ago













So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
– rider dragon
1 hour ago




So dummy am I, that is what I am willing to ask, it will overwritten in place if declared as global. @EliKorvigo
– rider dragon
1 hour ago












8 Answers
8






active

oldest

votes

















up vote
6
down vote



accepted










By "shadowing" the global character variable you are just hiding it from the main function, but it will still be part of the program.



It will live on.



If the variable was declared as static, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).



However, since the variable isn't declared as static, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).






share|improve this answer



























    up vote
    5
    down vote













    You have two separate variables named character: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main set to 'b', whose lifetime is that of its scope.



    The definition of character in main masks the definition at file scope, so only the latter is accessible.






    share|improve this answer




















    • How about if declared as static char character = 'b
      – rider dragon
      1 hour ago






    • 1




      @riderdragon For the variable in main, the static modifier gives it a lifetime of the whole program.
      – dbush
      1 hour ago










    • @riderdragon, the static declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
      – Myst
      1 hour ago


















    up vote
    4
    down vote













    It will live on (till the program dies, as any static-storage variable would) and you can still get to it:



    #include <stdio.h>
    char character = 'c';
    int main (void)

    char character = 'b';
    printf("The current value of head is '%c'n", character);

    extern char character;
    //in this scope, overrides the local `character` with
    //the global (extern?) one
    printf("The current value of head is '%c'n", character);

    printf("The current value of head is '%c'n", character);

    /*prints:
    The current value of head is 'b'
    The current value of head is 'c'
    The current value of head is 'b'
    */


    The local extern declaration doesn't work reliably/portably for static globals, though you can still get to them through pointers or through a separate function.




    (
    Why isn't static char character='c'; int main() char character='b'; extern char character; reliable with the global being static?



    6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).



    6.2.2p4:




    For an identifier declared with the storage-class specifier extern in
    a scope in which a prior declaration of that identifier is visible,31)
    if the prior declaration specifies internal or external linkage, the
    linkage of the identifier at the later declaration is the same as the
    linkage specified at the prior declaration. If no prior declaration is
    visible, or if the prior declaration specifies no linkage, then the
    identifier has external linkage.




    My clang 6.0.0 is accepting it with a static char character='b'; but my gcc 7.3.0 isn't.



    Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static too.
    )






    share|improve this answer


















    • 1




      Re “Doesn't work for static's though”: If the first declaration is changed to static char character…, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that “the prior declaration specifies internal or external linkage” and, for the other, “the prior declaration specifies no linkage.” I do not see a resolution of that in the standard.
      – Eric Postpischil
      1 hour ago











    • @EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
      – PSkocik
      1 hour ago











    • @EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
      – PSkocik
      1 hour ago






    • 1




      It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
      – Eric Postpischil
      54 mins ago


















    up vote
    3
    down vote













    Complement to the other answers:



    Try this and you'll understand:



    #include <stdio.h>

    char character = 'c';

    void Check()

    printf("Check: c = %cn", character);


    int main(void)

    char character = 'b';
    printf("The current value of head is %cn", character);
    Check();






    share|improve this answer
















    • 1




      Very elegant and usefull example.
      – manoliar
      1 hour ago










    • amazing example, ty.
      – rider dragon
      1 hour ago

















    up vote
    1
    down vote













    Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.



    But it could also depend on linker settings. Linker could optimize the unsused global variable out.






    share|improve this answer



























      up vote
      1
      down vote













      Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.



      A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.



      If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.






      share|improve this answer




















      • “Accessible” is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. “Access,” as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, “global” and “variable” are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
        – Eric Postpischil
        1 hour ago


















      up vote
      1
      down vote













      After the declaration of character in main, any reference to character in that function refers to that one, not the one at global scope. We call this shadowing.



      As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to



      #include <stdio.h>
      int main()

      printf("The current value of head is b");



      for example.






      share|improve this answer



























        up vote
        1
        down vote













        #include <stdio.h>
        char character = 'c';
        int main (void)

        char character = 'b';
        printf("The current value of head is %cn", character);
        printc();


        void printc()

        printf("%c", character);



        It makes it all clear. It is not destroyed it is just shadowed.



        Output:
        The current value of head is b

        c






        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',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          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%2f52875241%2fwill-a-value-live-perpetually-when-theres-no-reference-to-it%23new-answer', 'question_page');

          );

          Post as a guest






























          8 Answers
          8






          active

          oldest

          votes








          8 Answers
          8






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          By "shadowing" the global character variable you are just hiding it from the main function, but it will still be part of the program.



          It will live on.



          If the variable was declared as static, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).



          However, since the variable isn't declared as static, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).






          share|improve this answer
























            up vote
            6
            down vote



            accepted










            By "shadowing" the global character variable you are just hiding it from the main function, but it will still be part of the program.



            It will live on.



            If the variable was declared as static, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).



            However, since the variable isn't declared as static, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).






            share|improve this answer






















              up vote
              6
              down vote



              accepted







              up vote
              6
              down vote



              accepted






              By "shadowing" the global character variable you are just hiding it from the main function, but it will still be part of the program.



              It will live on.



              If the variable was declared as static, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).



              However, since the variable isn't declared as static, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).






              share|improve this answer












              By "shadowing" the global character variable you are just hiding it from the main function, but it will still be part of the program.



              It will live on.



              If the variable was declared as static, than the compiler might point out that the variable is never used and the code for the variable might never get emitted (it will be optimized away).



              However, since the variable isn't declared as static, the compiler will assume that the variable might be accessed externally and keep the code for the variable in place (which means that it will continue to require memory too).







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered 1 hour ago









              Myst

              11.4k22241




              11.4k22241






















                  up vote
                  5
                  down vote













                  You have two separate variables named character: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main set to 'b', whose lifetime is that of its scope.



                  The definition of character in main masks the definition at file scope, so only the latter is accessible.






                  share|improve this answer




















                  • How about if declared as static char character = 'b
                    – rider dragon
                    1 hour ago






                  • 1




                    @riderdragon For the variable in main, the static modifier gives it a lifetime of the whole program.
                    – dbush
                    1 hour ago










                  • @riderdragon, the static declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
                    – Myst
                    1 hour ago















                  up vote
                  5
                  down vote













                  You have two separate variables named character: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main set to 'b', whose lifetime is that of its scope.



                  The definition of character in main masks the definition at file scope, so only the latter is accessible.






                  share|improve this answer




















                  • How about if declared as static char character = 'b
                    – rider dragon
                    1 hour ago






                  • 1




                    @riderdragon For the variable in main, the static modifier gives it a lifetime of the whole program.
                    – dbush
                    1 hour ago










                  • @riderdragon, the static declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
                    – Myst
                    1 hour ago













                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  You have two separate variables named character: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main set to 'b', whose lifetime is that of its scope.



                  The definition of character in main masks the definition at file scope, so only the latter is accessible.






                  share|improve this answer












                  You have two separate variables named character: one at file scope set to 'c', whose lifetime is the lifetime of the program, and one in main set to 'b', whose lifetime is that of its scope.



                  The definition of character in main masks the definition at file scope, so only the latter is accessible.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  dbush

                  85.2k1090122




                  85.2k1090122











                  • How about if declared as static char character = 'b
                    – rider dragon
                    1 hour ago






                  • 1




                    @riderdragon For the variable in main, the static modifier gives it a lifetime of the whole program.
                    – dbush
                    1 hour ago










                  • @riderdragon, the static declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
                    – Myst
                    1 hour ago

















                  • How about if declared as static char character = 'b
                    – rider dragon
                    1 hour ago






                  • 1




                    @riderdragon For the variable in main, the static modifier gives it a lifetime of the whole program.
                    – dbush
                    1 hour ago










                  • @riderdragon, the static declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
                    – Myst
                    1 hour ago
















                  How about if declared as static char character = 'b
                  – rider dragon
                  1 hour ago




                  How about if declared as static char character = 'b
                  – rider dragon
                  1 hour ago




                  1




                  1




                  @riderdragon For the variable in main, the static modifier gives it a lifetime of the whole program.
                  – dbush
                  1 hour ago




                  @riderdragon For the variable in main, the static modifier gives it a lifetime of the whole program.
                  – dbush
                  1 hour ago












                  @riderdragon, the static declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
                  – Myst
                  1 hour ago





                  @riderdragon, the static declaration behaves differently, as mentioned in my answer, but it will not effect the lifetime, just the question "will the compiler optimize the variable away?".
                  – Myst
                  1 hour ago











                  up vote
                  4
                  down vote













                  It will live on (till the program dies, as any static-storage variable would) and you can still get to it:



                  #include <stdio.h>
                  char character = 'c';
                  int main (void)

                  char character = 'b';
                  printf("The current value of head is '%c'n", character);

                  extern char character;
                  //in this scope, overrides the local `character` with
                  //the global (extern?) one
                  printf("The current value of head is '%c'n", character);

                  printf("The current value of head is '%c'n", character);

                  /*prints:
                  The current value of head is 'b'
                  The current value of head is 'c'
                  The current value of head is 'b'
                  */


                  The local extern declaration doesn't work reliably/portably for static globals, though you can still get to them through pointers or through a separate function.




                  (
                  Why isn't static char character='c'; int main() char character='b'; extern char character; reliable with the global being static?



                  6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).



                  6.2.2p4:




                  For an identifier declared with the storage-class specifier extern in
                  a scope in which a prior declaration of that identifier is visible,31)
                  if the prior declaration specifies internal or external linkage, the
                  linkage of the identifier at the later declaration is the same as the
                  linkage specified at the prior declaration. If no prior declaration is
                  visible, or if the prior declaration specifies no linkage, then the
                  identifier has external linkage.




                  My clang 6.0.0 is accepting it with a static char character='b'; but my gcc 7.3.0 isn't.



                  Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static too.
                  )






                  share|improve this answer


















                  • 1




                    Re “Doesn't work for static's though”: If the first declaration is changed to static char character…, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that “the prior declaration specifies internal or external linkage” and, for the other, “the prior declaration specifies no linkage.” I do not see a resolution of that in the standard.
                    – Eric Postpischil
                    1 hour ago











                  • @EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
                    – PSkocik
                    1 hour ago











                  • @EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
                    – PSkocik
                    1 hour ago






                  • 1




                    It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
                    – Eric Postpischil
                    54 mins ago















                  up vote
                  4
                  down vote













                  It will live on (till the program dies, as any static-storage variable would) and you can still get to it:



                  #include <stdio.h>
                  char character = 'c';
                  int main (void)

                  char character = 'b';
                  printf("The current value of head is '%c'n", character);

                  extern char character;
                  //in this scope, overrides the local `character` with
                  //the global (extern?) one
                  printf("The current value of head is '%c'n", character);

                  printf("The current value of head is '%c'n", character);

                  /*prints:
                  The current value of head is 'b'
                  The current value of head is 'c'
                  The current value of head is 'b'
                  */


                  The local extern declaration doesn't work reliably/portably for static globals, though you can still get to them through pointers or through a separate function.




                  (
                  Why isn't static char character='c'; int main() char character='b'; extern char character; reliable with the global being static?



                  6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).



                  6.2.2p4:




                  For an identifier declared with the storage-class specifier extern in
                  a scope in which a prior declaration of that identifier is visible,31)
                  if the prior declaration specifies internal or external linkage, the
                  linkage of the identifier at the later declaration is the same as the
                  linkage specified at the prior declaration. If no prior declaration is
                  visible, or if the prior declaration specifies no linkage, then the
                  identifier has external linkage.




                  My clang 6.0.0 is accepting it with a static char character='b'; but my gcc 7.3.0 isn't.



                  Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static too.
                  )






                  share|improve this answer


















                  • 1




                    Re “Doesn't work for static's though”: If the first declaration is changed to static char character…, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that “the prior declaration specifies internal or external linkage” and, for the other, “the prior declaration specifies no linkage.” I do not see a resolution of that in the standard.
                    – Eric Postpischil
                    1 hour ago











                  • @EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
                    – PSkocik
                    1 hour ago











                  • @EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
                    – PSkocik
                    1 hour ago






                  • 1




                    It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
                    – Eric Postpischil
                    54 mins ago













                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  It will live on (till the program dies, as any static-storage variable would) and you can still get to it:



                  #include <stdio.h>
                  char character = 'c';
                  int main (void)

                  char character = 'b';
                  printf("The current value of head is '%c'n", character);

                  extern char character;
                  //in this scope, overrides the local `character` with
                  //the global (extern?) one
                  printf("The current value of head is '%c'n", character);

                  printf("The current value of head is '%c'n", character);

                  /*prints:
                  The current value of head is 'b'
                  The current value of head is 'c'
                  The current value of head is 'b'
                  */


                  The local extern declaration doesn't work reliably/portably for static globals, though you can still get to them through pointers or through a separate function.




                  (
                  Why isn't static char character='c'; int main() char character='b'; extern char character; reliable with the global being static?



                  6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).



                  6.2.2p4:




                  For an identifier declared with the storage-class specifier extern in
                  a scope in which a prior declaration of that identifier is visible,31)
                  if the prior declaration specifies internal or external linkage, the
                  linkage of the identifier at the later declaration is the same as the
                  linkage specified at the prior declaration. If no prior declaration is
                  visible, or if the prior declaration specifies no linkage, then the
                  identifier has external linkage.




                  My clang 6.0.0 is accepting it with a static char character='b'; but my gcc 7.3.0 isn't.



                  Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static too.
                  )






                  share|improve this answer














                  It will live on (till the program dies, as any static-storage variable would) and you can still get to it:



                  #include <stdio.h>
                  char character = 'c';
                  int main (void)

                  char character = 'b';
                  printf("The current value of head is '%c'n", character);

                  extern char character;
                  //in this scope, overrides the local `character` with
                  //the global (extern?) one
                  printf("The current value of head is '%c'n", character);

                  printf("The current value of head is '%c'n", character);

                  /*prints:
                  The current value of head is 'b'
                  The current value of head is 'c'
                  The current value of head is 'b'
                  */


                  The local extern declaration doesn't work reliably/portably for static globals, though you can still get to them through pointers or through a separate function.




                  (
                  Why isn't static char character='c'; int main() char character='b'; extern char character; reliable with the global being static?



                  6.2.2p4 seems like it wants to make it work for statics too, but the wording is ambiguous (a prior declaration has no linkage and another has static/extern linkage so what now?).



                  6.2.2p4:




                  For an identifier declared with the storage-class specifier extern in
                  a scope in which a prior declaration of that identifier is visible,31)
                  if the prior declaration specifies internal or external linkage, the
                  linkage of the identifier at the later declaration is the same as the
                  linkage specified at the prior declaration. If no prior declaration is
                  visible, or if the prior declaration specifies no linkage, then the
                  identifier has external linkage.




                  My clang 6.0.0 is accepting it with a static char character='b'; but my gcc 7.3.0 isn't.



                  Thanks to Eric Postpischil for pointing out the ambiguous possibility of this being usable with static too.
                  )







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 7 mins ago

























                  answered 1 hour ago









                  PSkocik

                  28.6k43963




                  28.6k43963







                  • 1




                    Re “Doesn't work for static's though”: If the first declaration is changed to static char character…, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that “the prior declaration specifies internal or external linkage” and, for the other, “the prior declaration specifies no linkage.” I do not see a resolution of that in the standard.
                    – Eric Postpischil
                    1 hour ago











                  • @EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
                    – PSkocik
                    1 hour ago











                  • @EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
                    – PSkocik
                    1 hour ago






                  • 1




                    It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
                    – Eric Postpischil
                    54 mins ago













                  • 1




                    Re “Doesn't work for static's though”: If the first declaration is changed to static char character…, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that “the prior declaration specifies internal or external linkage” and, for the other, “the prior declaration specifies no linkage.” I do not see a resolution of that in the standard.
                    – Eric Postpischil
                    1 hour ago











                  • @EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
                    – PSkocik
                    1 hour ago











                  • @EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
                    – PSkocik
                    1 hour ago






                  • 1




                    It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
                    – Eric Postpischil
                    54 mins ago








                  1




                  1




                  Re “Doesn't work for static's though”: If the first declaration is changed to static char character…, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that “the prior declaration specifies internal or external linkage” and, for the other, “the prior declaration specifies no linkage.” I do not see a resolution of that in the standard.
                  – Eric Postpischil
                  1 hour ago





                  Re “Doesn't work for static's though”: If the first declaration is changed to static char character…, the same output occurs, with Apple LLVM 9.1.0 clang-902-0.39.2. C 2018 6.2.2 4 is unclear on this. It provides some rules for extern on a later declaration when a prior declaration is visible. In this case, two prior declarations are visible. This makes it both true, for one prior declaration, that “the prior declaration specifies internal or external linkage” and, for the other, “the prior declaration specifies no linkage.” I do not see a resolution of that in the standard.
                  – Eric Postpischil
                  1 hour ago













                  @EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
                  – PSkocik
                  1 hour ago





                  @EricPostpischil Thanks for the comment. I didn't know about 6.2.2 4. It seems like the intention of it was that it should work for statics too, though it could've have been worded clearer (unambiguously). (Anyway, I've removed the "doesn't work for statics" paragraph.)
                  – PSkocik
                  1 hour ago













                  @EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
                  – PSkocik
                  1 hour ago




                  @EricPostpischil Readded it with more info. My clang is accepting it with static too, but my gcc is rejecting it with an error.
                  – PSkocik
                  1 hour ago




                  1




                  1




                  It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
                  – Eric Postpischil
                  54 mins ago





                  It is funny we still find ambiguities like this. Well, the C standard is only twenty years old. It may take a few more decades before we work out the kinks.
                  – Eric Postpischil
                  54 mins ago











                  up vote
                  3
                  down vote













                  Complement to the other answers:



                  Try this and you'll understand:



                  #include <stdio.h>

                  char character = 'c';

                  void Check()

                  printf("Check: c = %cn", character);


                  int main(void)

                  char character = 'b';
                  printf("The current value of head is %cn", character);
                  Check();






                  share|improve this answer
















                  • 1




                    Very elegant and usefull example.
                    – manoliar
                    1 hour ago










                  • amazing example, ty.
                    – rider dragon
                    1 hour ago














                  up vote
                  3
                  down vote













                  Complement to the other answers:



                  Try this and you'll understand:



                  #include <stdio.h>

                  char character = 'c';

                  void Check()

                  printf("Check: c = %cn", character);


                  int main(void)

                  char character = 'b';
                  printf("The current value of head is %cn", character);
                  Check();






                  share|improve this answer
















                  • 1




                    Very elegant and usefull example.
                    – manoliar
                    1 hour ago










                  • amazing example, ty.
                    – rider dragon
                    1 hour ago












                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  Complement to the other answers:



                  Try this and you'll understand:



                  #include <stdio.h>

                  char character = 'c';

                  void Check()

                  printf("Check: c = %cn", character);


                  int main(void)

                  char character = 'b';
                  printf("The current value of head is %cn", character);
                  Check();






                  share|improve this answer












                  Complement to the other answers:



                  Try this and you'll understand:



                  #include <stdio.h>

                  char character = 'c';

                  void Check()

                  printf("Check: c = %cn", character);


                  int main(void)

                  char character = 'b';
                  printf("The current value of head is %cn", character);
                  Check();







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Jabberwocky

                  25.2k93767




                  25.2k93767







                  • 1




                    Very elegant and usefull example.
                    – manoliar
                    1 hour ago










                  • amazing example, ty.
                    – rider dragon
                    1 hour ago












                  • 1




                    Very elegant and usefull example.
                    – manoliar
                    1 hour ago










                  • amazing example, ty.
                    – rider dragon
                    1 hour ago







                  1




                  1




                  Very elegant and usefull example.
                  – manoliar
                  1 hour ago




                  Very elegant and usefull example.
                  – manoliar
                  1 hour ago












                  amazing example, ty.
                  – rider dragon
                  1 hour ago




                  amazing example, ty.
                  – rider dragon
                  1 hour ago










                  up vote
                  1
                  down vote













                  Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.



                  But it could also depend on linker settings. Linker could optimize the unsused global variable out.






                  share|improve this answer
























                    up vote
                    1
                    down vote













                    Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.



                    But it could also depend on linker settings. Linker could optimize the unsused global variable out.






                    share|improve this answer






















                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.



                      But it could also depend on linker settings. Linker could optimize the unsused global variable out.






                      share|improve this answer












                      Normaly, the global variable will stay. Since your local variable only shadows the name, it doesn't affect the storage.



                      But it could also depend on linker settings. Linker could optimize the unsused global variable out.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 1 hour ago









                      Igor S.K.

                      584312




                      584312




















                          up vote
                          1
                          down vote













                          Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.



                          A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.



                          If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.






                          share|improve this answer




















                          • “Accessible” is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. “Access,” as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, “global” and “variable” are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
                            – Eric Postpischil
                            1 hour ago















                          up vote
                          1
                          down vote













                          Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.



                          A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.



                          If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.






                          share|improve this answer




















                          • “Accessible” is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. “Access,” as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, “global” and “variable” are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
                            – Eric Postpischil
                            1 hour ago













                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.



                          A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.



                          If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.






                          share|improve this answer












                          Although global variables exist from the start to the end of the execution of a program, they are not automatically accessible.



                          A global variable is accessible starting from the location in the file where the global variable is defined or declared until the end of the file.



                          If in a function scope one defines a variable with the same name, the global variable will exist, but will not be accessible.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 1 hour ago









                          Bogdan N.

                          1865




                          1865











                          • “Accessible” is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. “Access,” as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, “global” and “variable” are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
                            – Eric Postpischil
                            1 hour ago

















                          • “Accessible” is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. “Access,” as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, “global” and “variable” are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
                            – Eric Postpischil
                            1 hour ago
















                          “Accessible” is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. “Access,” as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, “global” and “variable” are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
                          – Eric Postpischil
                          1 hour ago





                          “Accessible” is not a good word to use for this. The C standard defines the issues here in terms of scope (and visibility) and linkage. “Access,” as defined in the standard, is reading or modifying an object, and the objects in question here are theoretically accessible, as there are ways to access an object without using its identifier. (For that matter, “global” and “variable” are imprecise. In the terms of the standard, an identifier has file scope, rather than global. And the scope applies to the identifier used to name the object, rather than variable.)
                          – Eric Postpischil
                          1 hour ago











                          up vote
                          1
                          down vote













                          After the declaration of character in main, any reference to character in that function refers to that one, not the one at global scope. We call this shadowing.



                          As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to



                          #include <stdio.h>
                          int main()

                          printf("The current value of head is b");



                          for example.






                          share|improve this answer
























                            up vote
                            1
                            down vote













                            After the declaration of character in main, any reference to character in that function refers to that one, not the one at global scope. We call this shadowing.



                            As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to



                            #include <stdio.h>
                            int main()

                            printf("The current value of head is b");



                            for example.






                            share|improve this answer






















                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              After the declaration of character in main, any reference to character in that function refers to that one, not the one at global scope. We call this shadowing.



                              As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to



                              #include <stdio.h>
                              int main()

                              printf("The current value of head is b");



                              for example.






                              share|improve this answer












                              After the declaration of character in main, any reference to character in that function refers to that one, not the one at global scope. We call this shadowing.



                              As for the effect on memory, you can't say due to the as-if rule adopted by the language: a compiler might optimise to



                              #include <stdio.h>
                              int main()

                              printf("The current value of head is b");



                              for example.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 1 hour ago









                              Bathsheba

                              170k26239362




                              170k26239362




















                                  up vote
                                  1
                                  down vote













                                  #include <stdio.h>
                                  char character = 'c';
                                  int main (void)

                                  char character = 'b';
                                  printf("The current value of head is %cn", character);
                                  printc();


                                  void printc()

                                  printf("%c", character);



                                  It makes it all clear. It is not destroyed it is just shadowed.



                                  Output:
                                  The current value of head is b

                                  c






                                  share|improve this answer


























                                    up vote
                                    1
                                    down vote













                                    #include <stdio.h>
                                    char character = 'c';
                                    int main (void)

                                    char character = 'b';
                                    printf("The current value of head is %cn", character);
                                    printc();


                                    void printc()

                                    printf("%c", character);



                                    It makes it all clear. It is not destroyed it is just shadowed.



                                    Output:
                                    The current value of head is b

                                    c






                                    share|improve this answer
























                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      #include <stdio.h>
                                      char character = 'c';
                                      int main (void)

                                      char character = 'b';
                                      printf("The current value of head is %cn", character);
                                      printc();


                                      void printc()

                                      printf("%c", character);



                                      It makes it all clear. It is not destroyed it is just shadowed.



                                      Output:
                                      The current value of head is b

                                      c






                                      share|improve this answer














                                      #include <stdio.h>
                                      char character = 'c';
                                      int main (void)

                                      char character = 'b';
                                      printf("The current value of head is %cn", character);
                                      printc();


                                      void printc()

                                      printf("%c", character);



                                      It makes it all clear. It is not destroyed it is just shadowed.



                                      Output:
                                      The current value of head is b

                                      c







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 1 hour ago

























                                      answered 1 hour ago









                                      curiousgeek

                                      335




                                      335



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52875241%2fwill-a-value-live-perpetually-when-theres-no-reference-to-it%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Comments

                                          Popular posts from this blog

                                          What does second last employer means? [closed]

                                          Installing NextGIS Connect into QGIS 3?

                                          One-line joke