Macro definition Simple macro
Format: #define Identifier substitution list
#define N 100int a[N];//N就是100
Macros with parameters
Format: #define identifier (x1, x2, x3,...... xn) Replace list
#define MAX(x, y) ((x) > (y) ? (x) : (y))
Note: When the program compiles, it will replace the uppercase identifier with the original expression, so the program will increase at compile time.
Advantages or Disadvantages
Advantages
① program may be a little faster
② Macros are more versatile
Disadvantages
① compiled code usually gets bigger
② cannot point to a macro with a pointer
③ macro may calculate its parameters more than once
Use of parentheses in macro definitions
① #define TWO_PI (2 * 3.1415)
② #define RES(x) ((x) * 10)
Cause: Because the macro is a substitution expression at compile time, many uncertainties
For example:
#define RES(x) (x * 10) //假如宏是这样定义的,参数没有括号int1);int110//编译器会替换成这样。与结果完全不对
Cancel macro
Format: #define N
Conditional compilation
#if 常量表达式 编译内容#endif
defined运算法仅用于预处理器,N必须是定义的宏#if defined(N)....#endif
The IFDEF directive tests whether an identifier has been defined as a macro
#ifdeft 标示符......#endif
#ifndef 标示符......#endif
"C-language summary" macro definition, preprocessing