How to print value of global variable and local variable having same name?

前端 未结 4 969
别那么骄傲
别那么骄傲 2020-12-03 15:00

Here is my code , I want to print 15 and 12 but due to instance member hiding the local value of a is getting printed twice.

#include                  


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 15:33

    Use the extern specifier in a new compound statement.

    This way:

    #include       
    
    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; 
    }
    

提交回复
热议问题