了解 C 预处理器宏的输出与行代码
understanding a C Preprocessor Macro's output vs a line code
#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 to
a given character, the next preprocessing token is the longest
sequence of characters that could constitute a preprocessing token.
所以这些标记 +++
被解析为 ++
和 +
。
带有宏的表达式
z1=AD(x1,++y1);
被编译器解析为
z1 = x1 + ++y1;
由于标记之间的逗号,编译器已经形成了这些标记集 x1
和 ++y1
。
所以这两个说法是不一样的
#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 to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.
所以这些标记 +++
被解析为 ++
和 +
。
带有宏的表达式
z1=AD(x1,++y1);
被编译器解析为
z1 = x1 + ++y1;
由于标记之间的逗号,编译器已经形成了这些标记集 x1
和 ++y1
。
所以这两个说法是不一样的