#define

#define tells the preprocessor, Whenever you encounter A, replace it with B before the compiler sees it. Look at the line from math.h again:

#​d​e​f​i​n​e​ ​M​_​P​I​ ​ ​ ​ ​ ​ ​ ​ ​3​.​1​4​1​5​9​2​6​5​3​5​8​9​7​9​3​2​3​8​4​6​2​6​4​3​3​8​3​2​7​9​5​0​2​8​8​

In the #define directive, you just separate the two parts (the token and its replacement) with whitespace.

#define can actually be used to make something like a function. In main.m, print the larger of two numbers:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​​u​0​3​c​0​ ​i​s​ ​%​f​"​,​ ​M​_​P​I​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​%​d​ ​i​s​ ​l​a​r​g​e​r​"​,​ ​M​A​X​(​1​0​,​ ​1​2​)​)​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

MAX is not a function; it is a #define. The most basic C version of MAX is:

#​d​e​f​i​n​e​ ​M​A​X​(​A​,​B​)​ ​ ​ ​ ​(​(​A​)​ ​>​ ​(​B​)​ ​?​ ​(​A​)​ ​:​ ​(​B​)​)​

So, by the time the compiler saw the line you just added, it looked like this:

N​S​L​o​g​(​@​"​%​d​ ​i​s​ ​l​a​r​g​e​r​"​,​ ​(​(​1​0​)​ ​>​ ​(​1​2​)​ ​?​ ​(​1​0​)​ ​:​ ​(​1​2​)​)​)​;​

When you use #define to do function-like stuff instead of simply substituting a value,you are creating a macro.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset