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