gpt4 book ai didi

c - 了解 C 预处理器宏的输出与行代码

转载 作者:行者123 更新时间:2023-12-04 02:28:08 25 4
gpt4 key购买 nike

#define AD(x,y) (x+y)
int main()
{
int x1=5,y1=2,z1;
int x2=5,y2=2,z2;
z1 = AD(x1,++y1);
z2 = (x2+++y2) ;
printf("%d %d %d\n",x1,y1,z1);
printf("%d %d %d\n",x2,y2,z2);
}

为什么输出不同?第一种情况是:5 3 8第二个是:6 2 7

最佳答案

这个表达式

z2=x2+++y2;

被编译器解析为

z2 = x2++ + y2;

来自C标准(6.4词法元素)

4 If the input stream has been parsed into preprocessing tokens up toa given character, the next preprocessing token is the longestsequence of characters that could constitute a preprocessing token.

因此这些标记 +++ 被解析为 +++。带有宏的表达式

z1=AD(x1,++y1);

被编译器解析为

z1 = x1 + ++y1;

由于标记之间的逗号,编译器已经形成了这些标记集 x1++y1

所以这两种说法是不同的。

关于c - 了解 C 预处理器宏的输出与行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65907318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com