如何打印具有相同名称的全局变量和局部变量的值?

时间:2022-05-06 03:37:15

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.

这是我的代码,我想打印15和12,但由于实例成员隐藏a的本地值被打印两次。

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

Why and is there any way to print it in c ? ... BTW I know it in c++.

为什么以及有没有办法在c中打印它? ...顺便说一下,我在c ++中知道它。

4 个解决方案

#1


20  

Use the extern specifier in a new compound statement.

在新的复合语句中使用extern说明符。

This way:

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

#2


2  

I know this doesn't directly answer your question, but the best way to do this is to change the name of the local variable so it doesn't conflict with the name of the global variable.

我知道这并没有直接回答你的问题,但最好的方法是更改​​局部变量的名称,使其不与全局变量的名称冲突。

If you have control of the code inside the function (i.e., you can add an extern declaration to make the global variable visible), then you can just as easily change the name of the variable.

如果您可以控制函数内部的代码(即,您可以添加extern声明以使全局变量可见),那么您可以轻松更改变量的名称。

It's impossible to tell what name would be better. In practice, the variables will undoubtedly have more descriptive names than a. The way they're used should give you some guidance about good names for them.

不可能分辨出哪个名字会更好。实际上,变量无疑具有比a更具描述性的名称。它们的使用方式应该为您提供一些关于它们的好名称的指导。

If they actually serve the same purpose, they probably don't both need to exist. You might remove variable that's local to main(), or, perhaps better, remove the global and pass the local (or its address) to other functions that need to access it.

如果他们实际上服务于同一目的,他们可能并不都需要存在。您可以删除main()的本地变量,或者更好的是删除全局变量并将本地(或其地址)传递给需要访问它的其他函数。

#3


1  

I think i found my answer in a way... it works

我想我在某种程度上找到了答案......它有效

#include <stdio.h>

int a = 5;


int main()
{
    int a=10;
    if(1)
    {
        extern int a;
        printf("global: %d\n", a);
    }
    printf("local: %d\n", a);
    return 0;
}

#4


-2  

add :: for global ambit

add :: for global ambit

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

#1


20  

Use the extern specifier in a new compound statement.

在新的复合语句中使用extern说明符。

This way:

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

#2


2  

I know this doesn't directly answer your question, but the best way to do this is to change the name of the local variable so it doesn't conflict with the name of the global variable.

我知道这并没有直接回答你的问题,但最好的方法是更改​​局部变量的名称,使其不与全局变量的名称冲突。

If you have control of the code inside the function (i.e., you can add an extern declaration to make the global variable visible), then you can just as easily change the name of the variable.

如果您可以控制函数内部的代码(即,您可以添加extern声明以使全局变量可见),那么您可以轻松更改变量的名称。

It's impossible to tell what name would be better. In practice, the variables will undoubtedly have more descriptive names than a. The way they're used should give you some guidance about good names for them.

不可能分辨出哪个名字会更好。实际上,变量无疑具有比a更具描述性的名称。它们的使用方式应该为您提供一些关于它们的好名称的指导。

If they actually serve the same purpose, they probably don't both need to exist. You might remove variable that's local to main(), or, perhaps better, remove the global and pass the local (or its address) to other functions that need to access it.

如果他们实际上服务于同一目的,他们可能并不都需要存在。您可以删除main()的本地变量,或者更好的是删除全局变量并将本地(或其地址)传递给需要访问它的其他函数。

#3


1  

I think i found my answer in a way... it works

我想我在某种程度上找到了答案......它有效

#include <stdio.h>

int a = 5;


int main()
{
    int a=10;
    if(1)
    {
        extern int a;
        printf("global: %d\n", a);
    }
    printf("local: %d\n", a);
    return 0;
}

#4


-2  

add :: for global ambit

add :: for global ambit

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