C programming language - pre-compiler directive

Pre-compiler directive (compiler implementation, not the C language content)

The main processing instructions starting with a #. #Include file contains codes such as copy, replacing #define for macros, conditional compilation like #if

When required pre-compiled: Always use a large body of code are not changed often.

Pretreatment process before compilation, and one of the tasks is the compilation of the grammar checker, grammar checker is not pretreated.


#define宏

Macro defines the type of problem does not exist, it is no type of argument. Only the characters were replaced, there is no type of security check.
Write a function outside the braces, the scope of its program, usually at the beginning of the file most.

C and C ++ language by default NULL replaced by 0

1) integrated debugging tools can not debug the macro constants.

2) only a simple replacement does not automatically brackets

3) the definition of the macro can not be nested.

4) the string "" forever does not contain macros.

5) does not allocate memory macro definitions, variable definitions to allocate memory.


Macro function, replace corresponding macro parameters statement, no longer a simple string replacement:

#define SQR(X) X*X

SQR(2+1):返回2+1*2+1 也就是5

SQR(2+1)/SQR(2+1) : 返回 2+1*2+1/2+1*2+1 返回7.5

# Is the macro parameter into a string operator ## is connected to two macro parameters operator.

Automatically distinguish the replacement operation parameters (when not the parameter name and operator, the character can not be replaced by alternative operator)

#define SQR(X) X*X   
SQR(1)替换成1

#define SQR(X) XY
SQR(任何数)替换成XY

#define STR(arg) #arg STR(hello)展开时为”hello”

#define NAME(y) name_##y NAME(1)展开为name_1

#undef: Cancel macro definition
#ifdef: If the macro is already defined


assert () macro

assert () is defined in a <assert.h> macros (i.e. define the header file).
Accepts a Boolean value that, if true (non-zero) continue to perform; false (that is, 0), then it Xianxiang stderr print an error message, and then terminate the program by calling abort


#include the header file is introduced, # if conditional compilation

Header file in turn allows include other header files, so that inevitably occurs repeatedly is a header file containing
e.g., to avoid being included my_head.h header, which may be used in the conditional compilation:

Conditional compilation (the conditional command preprocessor):

#ifndef _MY_HEAD_H   //如果该宏没有被定义
引入头文件
#define _MY_HEAD_H /*空宏*/  //定义空宏,就是定义成了空白符,用作标志
#elif
#else
#endif   #终止#if预处理命令


Published 46 original articles · won praise 15 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41850194/article/details/105315802