Original article address:
Http://blog.csdn.net/kindazrael/article/details/8108868
In C, Preprocessor is a powerful tool that makes your code easier to read and modify. With preprocessing code, you can redefine a part of the code to make your code more suitable for your style. Preprocessor is processed in advance before the code is compiled. The pre-processing code starts with a pound sign.
1. About
#define
#define
Declarations are mainly used to assign meaningful names to constants (or strings). For example, when you write a calendar program, you can define:
#define MONTHS_PER_YEAR 12
Note the format.#define
The command is followed by the constant name, followed by the assigned replacement value, separated by spaces, and the end does not require a semicolon.
# Define constant name substitution value
In general, it is a habit to write all the names of the preprocessed constants in upper case, and separate words with underscores ). The reason for doing so is that the compiler processes this section#define
When the code is used, spaces are automatically searched, and spaces are considered to be replaced by values. This is also the reason why there is no extra points at the end of each line of code, because if a semicolon is added, the semicolon itself will be considered as a part of the value.
A defined constant name is not a variable. Once defined, its value cannot be changed.#define
The Declaration is usually located at the top of a file, followed#import
After the file is imported and defined. Note that,#define
The declaration must be placed before the constant name is used (unlike the method that can be placed in any position in the file ).
Use#define
Another example is to store some meaningful values, such
# Define PI 3.141592654 // calculate the area of the circle using "Pi * radius"
This is much more convenient than writing everywhere 3.141592654. (The change is also much faster)
2. More advanced use
Note that the function of preprocessing code is equivalent to a "search and replace" function in the entire code before compilation. This means#define
There are many more complex and advanced uses.
Starting from 2πTWO_PI
It is defined as 2 π:
#define TWO_PI 2.0 * 3.141592645
Since we have previously defined π, we can also use the defined constant name in the definition to refer:
#define TWO_PI 2.0 * PI
In addition to constants, you can also use preprocessing to define any character or string in objective-C code, for example:
#define AND &&#define OR ||#define EQUALS ==if (y EQUALS 0 OR y EQUALS 1) // …
Rational Use#define
To enhance the readability of the Code. Compare the following two lines of code:
If (Year % 4 = 0 & amp; Year % 100! = 0) | (Year % 400 = 0) // or # define is_leap_year (Year % 4 = 0 & year % 100! = 0) | (Year % 400 = 0) if (is_leap_year) // The code is more readable.
A definition should usually be completed in a line of code. If you need to manually empty rows, enter a backslash (\) after each line to connect several lines of code into one line. For example:
#define IS_LEAP_YEAR(y) (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)if ( IS_LEAP_YEAR(currentYear) )
3. Macro
#define
Parameters can also be used in the declaration, and multiple parameters can be used; this feature is called "macro ":
For example:
#define SQUARE(x) ( (x) * (x) )
At this time,y = SQUARE(v + 1);
Equivalenty = ( (v + 1) * (v + 1) );
.
Two Parameters
For example, you can define:
#define CalcInterest(x,y) ( x * y )
Code:
int earnings = CalcInterest(10,5));
Multiple Parameters
For example, in the code, you need to frequently use[NSArray arrayWithObjects: object, ..., nil]
This command. Exploitation#define
, You can simplify the Code:
#define Array(FIRST, ...) [NSArray arrayWithObjects: FIRST, ##__VA_ARGS__, nil]
Where##__VA_ARGS__
All content represented by ellipsis.
4. # operators and # Operators
# The operator can generate a string in the C-language format. For example:
# Define string (x) # x // then string (testing) = "testing"
# Operator used to connect two strings. Suppose you have a set of variables,x1
Tox100
. If you want to print the value of a variable. You can define it as follows:
#define printxvar(n) printf("%i\n", x ## n)
Therefore,printxvar(20);
It will be recognizedprintf(“%i\n”, x20);
. In this case, you only need to enterprintxvar(n);
You can print outxn
.
Actually, ## the most common usage is described above##__VA_ARGS__
It can be used to capture all content represented by ellipsis.