C language 11th round: pre-processing command of the concentration camp
"Learning goals"
1. Macro definition
2. The file contains "processing
3. Conditional compilation
Pre-processing command: Can improve the design environment, improve the efficiency of programming.
There are three main functions: macro definition, file inclusion, and file compilation.
ANSI standard-defined C Language preprocessing instruction preview table
A: macro Definition
( a ) macro definition without parameters
Format: #define标识符 string
such as: #define PI 3.1415926
* Identifiers are called: macro names
* The process of replacing a macro name with a string at precompilation is: macro expansion.
* #define IS Macro definition command//Circle length # include <stdio.h> #define PI 3.1415926 int main (void) { float R; Radius float C; Circumference r= 3.0; c= pi* R; Ask for the circumference long printf ("%.3f\n", c); Keep 3 as Decimal
Ps:
(1) Macro names are generally uppercase letters, so that they are easy to distinguish. But it can also be lowercase.
(2) Using Macro name substitution can reduce the amount of code, easy to maintain
(3) The macro definition is not a C statement, so you do not have to add a semicolon at the end. If a semicolon is added, the macro name will replace the string and semicolon
#define NUM 123; There is a semicolon, the compiler will error!!
(4) The scope defined by the macro can be aborted with the #undef command, or its scope will end with the beginning of the definition of this file.
#undef的使用 #include<stdio.h> int main (void) { #define N- //n replaces the printf ("%d\n", n); #undef the scope of n//end N. If this statement is not indicated redefined #define N //n instead of printf ("%d\n", n); return 0; }
(5) macro definition, you can reference the defined macro name, you can layer displacement
Such as:
#include <stdio.h> #define R 3.0#define pi3.1415926#define c2*pi*r#define spi*r*r int main (void) { //print circle radius R, Perimeter c, area s printf ("r=%.2f\n", R); printf ("c=%.2f\n", C); printf ("s=%.2f\n", S); return 0;}
(6) characters in a string enclosed in a "" (double apostrophe) in a program are not displaced even if they are the same as the macro name.
Such as:
#include <stdio.h> #define stringhello,world! #define STRING "hello,world!" int main (void) { printf ("string") ; string is not replaced with hello,world! Putchar (' \ n '); NewLine printf (STRING); To be replaced by hello,world! return 0;}
Operation Result:
String
hello,world!
(7) macro definition is different from the definition of variable, macro definition only do character substitution, do not allocate storage space
( b ) macro definition with parameters
Role: the substitution of parameters
Format: #define宏名 (parameter table) string
[1] Add a macro with parameters
#include <stdio.h>//macros with parameters to replace # define ADD (A, B) ((a) + (b))//use parentheses for each variable to prevent the substitution of ambiguous int main (void) { int n= 10;
int m=; int sum= 0; Sum= ADD (n, m); printf ("%d\n", sum); return 0; }
[2] sizeof with parameters for macro substitution. The length of the array can be evaluated ( remember that the use of sizeof is limited !)
/* Instructions for use: the strlen () function can only be used to find the length of the string SIZEOF cannot be used as the original size of the array to pass the parameters */#include <stdio.h>//define sizeof macro # define sizeof (array) (sizeof (array)/sizeof (array[0])) int main (void) { int num= 0; int array[]= {1, 2,3, 3, 7, 5, 5, 6, 7, ten}; num= SIZEOF (array); printf ("The length of the array is%d\n", num); return 0; }
Operation Result:
The length of the array is 13
Ps:
(1) For macros with parameters, the substitution is made from left to there, if the character in the string is not a parameter (such as + in A + B) to be preserved.
(2) You cannot add a space between the macro name defined by the macro and the parentheses of the parameter, or it will be treated as part of the string
Note: The difference between a function and a macro
(1) The number of times a macro is used, the length of the program grows after the macro expands, and the function's call does not.
(2) macro substitution does not occupy the running time, only the compilation time, while the function takes up the running time (that is, allocation unit, value transfer, return, etc.)
B: file contains " processing
"file contains" Processing: One source file can be included in all of the other source files.
Format: (a) #include "file name"
(b) #include < file name >
The difference between the two formats: using angle brackets means to find in the directory of contained files (that is, the library file), which is set by the user when setting up the environment, but not in the source file directory; Using double quotation marks is the first to find in the current source file directory, if not found in the inclusion directory (library file) to find.
Ps:
(1) An # include command can contain only one source file, multiple source files with multiple # include commands
(2) If file 1 to include file 2, and file 2 to include file 3, you can include 1 # include in file two contains the file 2 and file 3, and the file 3 must appear before the file 2.
(3) files can be nested, that is, an include file can contain a contained file
(4) The contained file (file2.h) and the file it resides in (i.e., the source file with the # include command file2.c), has become the same file (not two files) after precompilation. Therefore, if there is a global static variable in file2.h, it is also valid in the File1.h file and does not have to be declared with extern.
C: Conditional Compilation
Conditional compilation: A condition that specifies compilation for part of the content, and that it satisfies certain conditions before compiling.
Format:
(1)
#ifdef identifiers
Procedure Section 1
#else
Procedure Section 2
#endif
Function: If the identifier is defined by a # define, the program Segment 1 is compiled, whereas the program segment 2 is compiled. If there is no program segment 2, you can omit the #else.
#include <stdio.h> #define R3.0 //define R int main (void) { #ifdef R //similar to If-else printf ("r\n defined") ; #else printf ("No r\n Defined"); #endif //Conditional judgment end return 0;}
(2)
#ifndef identifiers
Procedure Section 1
#else
Procedure Section 2
#endif
Function: If the identifier is not defined by a # define, the program Segment 1 is compiled, and the program segment 2 is compiled instead.
(3)
#if constant expression
Procedure Section 1
#else
Procedure Section 2
#endif
Action: If the value of the expression is true (that is, it is not 0), the program Segment 1 is compiled, whereas the program segment 2 is compiled.
#include <stdio.h> #define R 3.0 //define R int main (void) { #ifndefR ///similar to If-else printf ("No r\n defined" ); #else printf ("already defined r\n"); #endif //Conditional judgment end Return0;}
"Smile at the Fingertips" error is unavoidable, hope to get everyone's correction ^ ^
When reproduced, keep the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself
C Language 11th round: pre-processing command of the concentration camp