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

前端 未结 8 1253
鱼传尺愫
鱼传尺愫 2021-02-13 07:02

Suppose the following minimal code:

#include 
char character = \'c\';
int main (void)
{
    char character = \'b\';
    printf(\"The current value         


        
8条回答
  •  一向
    一向 (楼主)
    2021-02-13 07:49

    "shadowing" the global character variable hides the variable from the main function, but it will still be a part of the program.

    If character variable was declared as static, then the compiler might warn that character variable is never used and character will be optimized away.

    However, character variable is not declared as static; the compiler will assume that character variable might be accessed externally and keep character variable in the memory.

    EDIT:

    As noted by @Deduplicator, linker optimizations and settings could omit the variable from the final executable, if permitted. However, this is an edge case that won't happen "automatically".

提交回复
热议问题