重新定义预处理程序指令

编程入门 行业动态 更新时间:2024-10-21 11:34:35
本文介绍了重新定义预处理程序指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

的输出是什么 #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.

更多推荐

重新定义预处理程序指令

本文发布于:2023-11-14 00:37:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   定义   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!