定义宏定义的错误 [英] error with define macro definition

查看:156
本文介绍了定义宏定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 #define 函数时,我观察到一些奇怪。
在下面的代码中,如果我从给出 '10'的值 i > i 的输出为 132 。但是,如果我通过注释10,12和13行声明 i = 10 ,那么我的输出是 144 。谁能解释我这是怎么回事?
提前感谢

when i am using #define function,I observe something bizarre. In the below code if I gave i value as '10' from input i got the output as 132. However if I declare i=10 by commenting 10,12 and 13 lines then my output is 144. can anyone explain me how this is happening? thanks in advance

#include <iostream>
using namespace std;

#define Double(X) X*X 

int main()
{

 //int i=10;
 int i;

cout<<"Enter the i values:" <<endl;
cin>>i;

cout<<"Values is:"<<Double(++i)<<endl;

 return 0;
}


推荐答案

Double(++ i)将扩展为 ++ i * ++ i 。在此表达式中, i 被修改两次,而没有介入的序列点,即未定义的行为

Double(++i) will expand to ++i * ++i. In this expression, i is modified twice without an intervening sequence point, which is undefined behavior.

阅读:那么,使用宏有什么问题?

这篇关于定义宏定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆