如何打印的全局变量和局部变量有相同名称的价值? [英] How to print value of global variable and local variable having same name?
本文介绍了如何打印的全局变量和局部变量有相同名称的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的code,我想打印15和12,但由于实例成员隐藏的本地值是越来越打印两次。
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 <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;
}
为什么,是有什么办法可以打印在C? ......顺便说一句,我知道它在C ++中。
Why and is there any way to print it in c ? ... BTW I know it in c++.
推荐答案
使用的extern
说明在一个新的复合语句。
Use the extern
specifier in a new compound statement.
这方式:
#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;
}
这篇关于如何打印的全局变量和局部变量有相同名称的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文