首页 > 其他 > 详细

ObjC宏定义小细节

时间:2016-07-05 18:27:04      阅读:120      评论:0      收藏:0      [点我收藏+]

Macros

A definition that takes arguments, particularly more than one, is often known as a macro:

#define SQUARE(x) x * x

Incidentally, the previous definition leads to an interesting problem. What would happen with this line:

y = SQUARE(v + 1);

Because of the literal substitution, what actually gets compiled is y = v + 1 * v + 1;, which algebraically simplifies to y = 2 * v + 1;. Therefore, the macro should be properly define as

#define SQUARE(x) ( (x) * (x) )

Therefore, y = SQUARE(v + 1); becomes y = ( (v + 1) * (v + 1) );.

The rule of thumb is to enclose each argument within the definition in parentheses, which should avoid the issue above.

原来宏定义的时候,会发生这种情况。。。以后定义宏四则运算的时候要注意运算优先级了。

原文地址:http://www.binpress.com/tutorial/learn-objectivec-the-preprocessor/69

ObjC宏定义小细节

原文:http://www.cnblogs.com/kptanjunhao/p/5644337.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!