In general, all lines in the source program are compiled. However, sometimes it is desirable to compile a part of the content only if certain conditions are met, that is, to specify the conditions for the compilation of some content, which is "conditional compilation". Sometimes, you want to compile a set of statements when a condition is met, and then compile another set of statements when the condition is not met.
I saw it today.
A # define is followed only by a "parameter"
Defines a macro and replaces it with an empty string (that is, delete) during preprocessing.
This is done primarily to mark certain content so that the program reader can clearly mark the meaning without affecting the compiled source code.
In addition, usually these tokens can be #ifdef, #ifndef检测到 of preprocessor commands for conditional compilation. One important usage is that multiple file compilations prevent header files from being repeatedly included, such as:
Header.h:
#ifndef _header_
#define _header_
/*something*/
#endif
A.c
#include "header.h"
/*something*/
Main.c
#include "header.h"
/*something*/
So no matter which. c file is compiled first, contains header.h later, after entering # # # header.h in the _header_ defined, then the code between #ifndef _header_ and #endif will not be compiled repeatedly, The target code for HEADER.H only exists in the target code file of one of the source files, and the linker links the target files compiled by the two source files without generating duplicate-defined link errors due to duplicate compilation.
We can understand it from the Scarlet Letter section.
explain
Specifically, this is the pre-compilation period, our macro to do macro replacement, contains the header file to replace the code inside the header file, but note that when you write a large C language problem pieces, you may write into a few small C language files
Compiled separately, and then become a subject after linking. The problem is that when you write a small C language file, you may have a duplicate header file included. For example, you include the header file in the A.C zhangfei.h the header file in the header file B.C zhangfei.h
But the problem is, because when you link to a file after compilation, there is a duplicate of the header file. Of course, if you write as a C file in the beginning, there will be no problem. But the project is too big to write just one file. How do we solve that
is to write a file yourself, in which we use pre-compiled some grammatical means. Make a header file compile only once. The specific syntax implementation is the code written above.
The macro definition is replaced by the compiler before it is compiled, as you would say with only one identifier, and if you use this identifier in your code, it will be replaced by NULL, which is equivalent to no Add.
Used to make the compiler switch.
#define DEBUG
#ifdefine DEBUG
Your code (output debug information, etc.)
#endif
When debug is defined, the Your Code section is compiled, and if debug is undefined, the compiler ignores the Your code section.
Pre-compiling those things define a "parameter" followed by a