#define is often seen in the code, #ifdef
#define: The apparent constant general instruction length is limited to one line of code, except for the \ (backslash line) can be extended to the next line.
In general, each # define row consists of three parts.
- The first part is the # define directive itself
- The second part is the selected abbreviation, which is called macro. There are no spaces allowed in the name of the macro! Use only letters, underscores (_), and numbers, and the first character cannot be a number
- The third part (#define行除了第一第二部分外的部分). Called a replacement list or principal.
- after the preprocessor finds an instance of the macro in the program, it always replaces the macro with a body, with one exception .
macros can also use parameters to create class function macros that are similar in shape and function to functions. The parameters of the macro are enclosed in parentheses.
The parameters of the macro are not exactly the same as the parameters of the function
When the program runs, the function call passes the value of the parameter to the function, and before compiling, the macro calls the language symbol of the argument to the program.
#define Square (x) x*xx=4evaluating Square (x): The result is a //aevaluating square (2): The result is 4 B evaluating SQUARE (x+2): The result is a //c evaluating 100/square (2): The result is a //d
For A, B results are the same. But the other results are different.
The original x*x parameter in C should be x+2*x+2
D is 100/2*2, so we should count 100/2 first.
Conditional compilation:
#ifdef MAVIS
#define X 5
#else
#define X 6
#endif
#ifdef指令说明, if the preprocessor has defined the following marker MAVIS, execute all instructions and compile the code until the next #else or #endif appears. If no subsequent glyph is defined Mavis executes the instructions followed by #else and compiles until #endif. Similar to the if else in C. The #ifndef, in other words, determines whether the subsequent glyph is undefined, and executes the following if the subsequent marker is undefined.
#if
#elif more like the IF in C,
These are generally followed by constant integer expressions, except that 0 is true. Defined is a preprocessing operator//defined (para), returns true if his arguments have been defined with # define, otherwise returns 0
C Primer Plus reading notes-preprocessor directives