#include <stdio.h>
#include <stdlib.h>
#define PATRAT(x) x*x+x;
int main()
{
int x,y,z,t;
x = 104;
y = PATRAT(x - 1);
z = PATRAT(y++);
t = ++PATRAT(x);
printf ("%d %d %d", y, z, t);
return 1;
}
给出这段代码,结果是 y = 105,z = 10506,t = 11130,我无法理解为什么。能解释一下吗?
最佳答案
预处理器将其扩展如下:
y = x - 1*x - 1+x - 1;;
z = y++*y+++y++;;
t = ++x*x+x;;
在我们将其设置为人类可读后,它会显示:
y = x - (1 * x) - 1 + x - 1;
z = (y++ * y++) + y++;
t = (++x * x) + x;
这是未定义的行为 because we're modifying y
without sequence points in between ,因此任何输出都是可能的。
关于c - 在c中用#Define定义的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35321663/