宏评价 [英] Evaluation of Macro
问题描述
可能重复:结果
未定义行为和顺序点
我有理解下面的宏麻烦:
I am having trouble in understanding the following macro:
#define CUBE(x) ((x)*(x)*(x))
我的code:
int y=5;
print("Cube = %d",CUBE(++y));
这件code打印512(使用Microsoft Visual Studio)
This piece of code prints 512 (Using Microsoft Visual Studio)
我期待这个打印:((++ 5)*(++ 6)*(++ 7))=(6 * 7 * 8)= 336
。结果
这是怎么评估?
I was expecting this to print: ((++5)*(++6)*(++7)) = (6*7*8) = 336
.
How this is evaluated?
另外,同一code,采用开发C ++编译器编译,其结果是392!
Also the same code was compiled using Dev C++ compiler and the result is 392!!!
有人能解释为什么两种编译器显示完全相同code不同的结果?
Can someone explain why two compilers show different results for exactly same code?
这是怎么回事 ++是
评估?
推荐答案
在展开宏 y的值
被修改不止一次没有插入顺序点。结果
这就是未定义行为
When the macro is expanded the value of y
gets modified more than once without an intervening sequence point.
This is Undefined Behavior.
未定义行为意味着任何行为是可能的,你的程序是不是一个有效的C程序。结果
因此,你看到不同的编译器不同的输出。
Undefined Behavior means that any behavior is possible, Your program is not a valid c program.
Hence you see different outputs on different compilers.
这篇关于宏评价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!