Preprocessor:
Before compiling the program, the preprocessor examines the program, and according to the preprocessor directives used in the program, the preprocessor replaces the abbreviations in the program with the content represented by the symbol abbreviations.
The preprocessor can be based on containing other files and can optionally have the compiler handle which code, the preprocessor does not understand C, it is generally accepting some text and converting it to other text
1. Usage of #define
Defined:
The macro #define preprocessing directives can appear anywhere in the source file where the directive definition acts to start from where the definition appears until the end of the file.
When a preprocessor discovers an instance of a macro in a program, it always replaces the macro with an entity, and the process of changing from a macro to the final replacement text is called a macro expansion.
After the preprocessor discovers a macro in the program, it replaces the macro with its equivalent replacement text. If the character also includes macros, continue to replace those macros.
The const keyword is supported by C, which does provide a more flexible way to create constants, using const to create global variables and local variables, numeric constants, array constants, and struct constants, while macro constants can be used to specify the standard array size and act as a const-worthy initialization value.
#define LIMIT 20
const int LIM = 50;
static int data1[limit]; Legal
static int Data2[lim]; Invalid
const int LIM2 = 2 * LIMIT; Legal
const int LIM3 =; Invalid
(1) Simple usage
#define MaxTime
A simple maxtime is defined, it represents 1000, if it is written inside the program
if (I<maxtime) {...}
The compiler replaces the maxtime with 1000 before processing the code.
Such a definition looks similar to a normal const definition, but it is also different because the definition of define is more like a simple text substitution than a volume.
(2) function definition ( always note that # define is just a text substitution without any compilation processing )
define can accept some parameters like a function, as follows
#define MAX (x, y) > (y)? (x):(y);
This definition will return the larger of the two numbers, see? Because this "function" does not have a type check, it is like a function template, of course, it is absolutely not as safe as a template. Can be used as a simple template.
but there are pitfalls to doing so, examples are:
#define ADD (A, b) a+b;
There is no problem when it comes to general use, but if you encounter problems such as C * ADD (b) * d, the algebraic intent is to a+b and then to multiply with c,d, but since the use of define (which is just a simple substitution), the equation actually becomes
c*a + b*d
also give an example:
#define PIN (int*);
pin A, b;
The original intention is that A and B are both int type pointers, but actually become int* a, b;
A is an int pointer, and b is a variable of type int.
This is the use of TypeDef instead of define, so that both A and B are int pointers.
So when we define it, we develop a good habit and suggest that all levels be bracketed.
(3) #define Multi-line definition
Define can replace multiple lines of code, such as the macro definition in MFC (very classic, although it makes people look disgusting)
#define MACRO (arg1, arg2) do {/
/* declarations */
STMT1; /
STMT2; /
/* ... */ /
} while (0)/* (no trailing;) */
The key is to add a "/" to each line break
(4). In the large-scale development process, especially cross-platform and system software, the most important function of define is conditional compilation.
#ifdef WINDOWS
#endif
#ifdef LINUX
#endif
#ifdef XXX ... (#else) ... #endif
For example #ifdef dv22_aux_input
#define Aux_mode 3
#else
#define Auy_mode 3
#endif
#ifndef XXX ... (#else) ... #endif
Compile the environment with a # define setting at compile time
(5). How to define a macro, cancel a macro
//defining Macros
#define [MacroName] [macrovalue]
//Suppress macro
#undef [MacroName]
normal macro
#define PI (3.1415926)
macros with parameters
#define MAX (b) ((a) > (b)? (a), (b)
(6) The header file (. h) can be Beatles file or C file contains
Repeat include (repeat definition)
Because the header file contains can be nested, then the C file may contain multiple times the same header file, there may be a duplicate definition of the problem.
Avoid duplicate inclusions with conditional compilation switches (duplicate definitions)
For example
#ifndef __headerfilexxx__
# define __headerfilexxx__
...
File contents
...
#endif
This feature can also use the #pargam once directive instead
(7) three special symbols in the Define: #,##,#@
===========================================================
1. #define Conn(x,y) x##y
2. #define tochar(x) #@x
3. #define ToString(x) #X
(1) What does the x# #y mean? Represents x connection Y, for example:
1. int n = Conn(123,456); / * results are n=123456;*/
2. Char* str = Conn("asdf", "ADF"); / * The result is str = "ASDFADF"; * /
(2) to see #@x , in fact, is to give X plus single quotation marks, the result is a const char. For example,
char a = ToChar (1); The result is a= ' 1 ';
Do a cross-border test. Char a = ToChar (123); the result is wrong;
But if your parameter exceeds four characters, the compiler will give you an error!
Error C2015:too many characters in Constant:p
(3) Finally, look at #x, you know, he's giving X double quotes.
char* str = ToString (123132); str= "123132";
Selection of macros and functions:
The choice between the macro and the function is actually the tradeoff between the time and the space, the macro produces inline code, that is, in the program generated statements, if the use of macros 20 times will be 20 lines of code into the program, if the use of the function 20 times, then only a copy of the function statement, thus saving space, on the other hand, The control of the program must be transferred to the function and then returned to the calling program, so it takes more time than the inline code.
A little something about the macro does not check the variable type in it
Predefined macros for C standard
_ _date_ _ Date of preprocessing ("MMM DD yyy" in the form of string literals)
_ _file_ _ Represents the string literal of the current source code file name
_ _line_ _ represents the integer constant of the line number in the current source code file
_ _stdc_ _ When set to 1 indicates that the implementation follows the C standard
_ _stdc_hosted_ _ in the native environment set to 1, otherwise set to 0
_ _stdc_version_ _ in C99 is set to 199901L
_ _time_ _ Source file compilation time format is "HH:MM:SS"
2. #include
(1) Two forms of use
#include <stdio.h>
#include "myStuuff.h"
In UNIX systems, the angle brackets tell the preprocessor to look for files in one or more standard system directories, double quotation marks tell the preprocessor to look for files in the current directory, and then the standard location is selected in the file
(2) using the header file
The most common forms of header files include:
A. Apparent constants
B. Macro functions
C. function declaration
d. Structure template definition
E. Type definition
c Preprocessor and preprocessor directives