重新定义预处理程序指令 [英] redefinition of preprocessor directive

查看:77
本文介绍了重新定义预处理程序指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


的输出是什么
#define A 10

int main()
{
#define A 20
cout<< A;
返回0;
}

what is the output of

#define A 10

int main()
{
#define A 20
cout<<A;
return 0;
}

推荐答案

重新定义后,A的值当然是20.因此cout将(在正确编写的程序中)输出20.
The value of A, after redefinition, is 20 of course. Hence cout would (in a properly written program) output 20.


这里A是一个宏,它被定义了两次.一项在全球范围内,一项在本地范围内.

宏在出现标识符时会被替换,并且不受块结构的影响.因此,在上述程序中,在cout期间,编译器尝试将A替换为提供的值,但发现为它定义了两个值,一个在全局范围内,另一个在局部范围内.因此会发出警告
警告C4005:"A":宏重新定义

因此,如果您使用的是Visual Studio编译器,则它将打印本地#define值,并且我们将获得20作为输出.
Here A is a macro and it is defined two times. One in global and one in local scope.

Macro gets replaced at any occurrences of identifier and it is not affected by block structure. So in above program during cout, compiler tries to replace A with provided value but it founds that are two values defined for it one in global and other in local scope. So it gives warning
warning C4005: ''A'' : macro redefinition

So if you are using visual studio compiler then it will print local #define value and we will get 20 as output.


它必须为20 :),我希望您在收到警告编译.
it must be 20 :), i hope u will get a warning during compilation.


这篇关于重新定义预处理程序指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆