Compilation preprocessing
#define可以定义宏. Macros can take parameters, called macros with parameters, whose arguments are called macro parameters.
#undef can remove a macro definition.
1 /*2 Macro Exercises3 */4 5#include <stdio.h>6 #defineNEG (r) r = 0-r7 8 Main ()9 {Ten intValue =4; One NEG (value); Aprintf"value is%d\n", value); -}
1 /*2 Macro Exercises3 */4#include <stdio.h>5 #defineSTR (R) #r6 #definePTR (r) p_# #r7 Main ()8 {9printf"STR (3 + 6) is%s\n", STR (3+6));//printf ("STR (3 + 6) is%s\n", "3 + 6");Ten intValue, *ptr (value) = &value;//int value, *p_value = &value; One*ptr (value) =4; Aprintf"*ptr (value) is%d\n", *PTR (value)); -}
1 /*Macro Exercises2 Macros with Parameters3 4 find the larger of the two numbers*/5 6#include <stdio.h>7 #defineN 28 #defineMAX (A, b) a > B? A:b9 Main ()Ten { One intIntegers[n] = {0}; A - -scanf"%d%d", integers, integers +1 ); theprintf"integers[0]>integers[1]?integers[0]:integers[1] Output results%d\n", integers[0]>integers[1]?integers[0]:integers[1] ); -printf"#define MAX (A, b) a > B? a:b\n calculation results are%d\n", MAX (*integers, * (integers +1))); - -}
1 /*2 * Macro Exercise3 */4 5#include <stdio.h>6 7 #definePI 3.1415926//if the macro pi is not declared (defined) in the source program, we can also define the macro via the GCC option-D8 9 #defineCIRCLE (r) 2 * PI * rTen One intMain () A { - intRadius =0; -printf"Please input radius:"); thescanf"%d", &radius); -printf"Rounder of the circle:%g", CIRCLE (RADIUS)); -}
1 /*2 Macro Exercises3 */4 5 #defineSECPH (60 * 60 * 60)//add parentheses to prevent inappropriate occurrence of computational priority issues6 7 #defineNEG (n)-N8#include <stdio.h>9 Ten Main () One { A -printf"7200/secph is%d\n .",7200/secph); -printf"#define NEG (n) is%\n", NEG (3)); the //printf ("#define NEG (N) is%\n", NEG (3, 8));//preprocessing phase error: the macro "NEG" passed 2 parameters, but only 1 - -printf"#define NEG (n) is%\n", NEG (3+8)); -printf"#define NEG (n) is%\n", NEG (3+8))); +}
For macros with parameters, we should note:
/*
Poorly defined, parametric macros tend to cause unexpected substitution of macros during preprocessing.
To avoid unintended substitutions of macros during preprocessing, we require:
All the parameters of a macro are enclosed in parentheses when used, and the result of the macro is surrounded by parentheses, with parentheses to prevent inappropriate occurrences of the calculation priority problem.
Do not use the calculated result of self-increment as a macro parameter.
*/
#include <stdio.h>
#define SECPH (60 * 60 * 60)
#define NEG (N) 0-(N)
#define SQUARE (n) (n) * (n)
Main ()
{
printf ("7200/SECPH is%d\n", 7200/SECPH);
printf ("NEG (3) is%\n", NEG (3));
printf ("NEG (3 + 8) is%\n", NEG (3 + 8));
printf ("Square (4) is%d\n", Square (4));
int value = 4;
printf ("Square (++value) is%d\n", Square (++value));
}
Attached: predefined macro exercises for common compilers
1 /*2 C-language pre-defined macro exercises3 */4 5#include <stdio.h>6 7 intMain ()8 {9printf"line number is%d\n", __line__);//implementation prints out the current line numberTenprintf"filename is%s\n", __file__); Oneprintf"Compile date is%s\n", __date__); Aprintf"compile time is%s\n", __time__); -printf"%SC standard \ n", __stdc__?"Meet":"does not meet");//The preprocessing result of macro __stdc__ will be a logical expression that evaluates to True if the C standard is met. - the}
Conditional compilation. Use pre-processing directives # If ... #endif可以实现按条件编译. #else ...
Defined macro name is a logical operation expression, when the macro name has been defined the result of the operation is true
/*conditional Compilation*/#include<stdio.h>//#define One//#defineintMain () {//#ifdef One#ifDefined One//#ifndef//#if!definedprintf"1\n");#elseprintf ("2\n");#endif}
Logical Non! Logic and && logic or | |
Conditional compilation preprocessing Directive #ifdef macro name is more concise than # if defined macro name, #ifndef macro name is more concise than #if!defined macro name.
Standard C Episode 7