码迷,mamicode.com
首页 > 其他好文 > 详细

ObjC宏定义小细节

时间:2016-07-05 18:27:38      阅读:269      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!