C preprocessing functions are provided:
- Macro definition
- File contains
- Conditional compilation
In order to distinguish its statement she c, command often the symbol "#" at the beginning.
Macro definition
#define identifier string
can avoid repeated input character string, without the back; valid macro definition range is all default. #Undef may also be used to terminate the macro definition area.
Without parameters
macro expansion program into the
parameter included
#define S (a, b) a * b
#include<stdio.h>
#define PI 3.1415
#define S(r) PI*r*r
int main()
{
int a;
float area;
scanf("%d",&a);
area=S(a);
printf("半径为%d的圆面积为%f",a,area);
}
Emphasize here that instead of arguments a parameter r, when the argument contains a number, priority attention parentheses.
Relationship between macros and function calls parameters:
1. the function call, the calculated value is the first argument expression, then substituting the parameters.
2. Function arguments parameter defines its type, the macro definition need.
File contains processing
I.e., a source code for the entire contents of file can further comprise a source file together. Common #include command is used to implement the operation.
When you often have to use some fixed data can be written to a file, write a direct reference to other documents.
Note: Use the file containing processed premise is to create a project, so that they can use each other in a project.
Conditional compilation
If you tell a function declaration twice, it will error. So after the write function declarations in header files, if cited twice this header file will error. #ifndef #define macro is used to avoid duplicate file header contains
Form: # if- Program 1 - # else- program 2- # endif
function: if the #if conditional expression is true, then the program 1 is selected, otherwise the program 2 is selected.
#include<stdio.h>
#define RESULT 0//定义 RESULT 为 0
int main (void)
{
#if !RESULT //或者 0==RESULT
printf("It's False!\n");
#else
printf("It's True!\n");
#endif //标志结束#if
return 0;
}
2. # ifndef- identifier - # define- identifier replacement list - # endif
functions: general used to detect whether the program has been defined as an identifier of the macro name, if not define this macro, the macro is defined, #define to start and select from among the block #endif; if defined, the definition of the symbol will not be repeated, and the corresponding block is not selected.