Today, we have compiled some # define usage to share with you!
1. Simple define Definition
#define MAXTIME 1000
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, and the problem is particularly evident below.
"Function definition" of 2.define
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. Single-line definition of macros
#define A (x) t_# #x
#define B (x) #@x
#define C (x) #x
We assume that: X=1, there are:
A (1)------〉t_1
B (1)------' 1 '
C (1)------"1"
(Refer to Hustli's article here)
Multi-line definition for 3.define
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
Excerpt from http://www.blog.edu.cn/user1/16293/archives/2005/115370.shtml patched a few bugs
4. In the large-scale development process, especially in cross-platform and system software, the most important function of define is conditional compilation.
It is:
#ifdef WINDOWS
......
......
#endif
#ifdef LINUX
......
......
#endif
Compile environment can be set by # define at compile time
5. How to define macros, cancel macros
Defining macros
#define [MacroName] [Macrovalue]
Cancel macro
#undef [MacroName]
Normal macro
#define PI (3.1415926)
Macros with parameters
#define MAX (a) > (b)? (a), (b))
The key is to be very prone to errors, including differences in machine and human understanding, and so on.
6. Conditional compilation
#ifdef XXX ... (#else) ... #endif
For example
#ifdef Dv22_aux_input
#define AUX_MODE 3
#else
#define AUY_MODE 3
#endif
#ifndef XXX ... (#else) ... #endif
7. The header file (. h) can be contained in a Beatles file or a C file;
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
These are just some of the things I've collected from the web about define, which may not be comprehensive, and the use of # define is a controversial one, and if you're interested in the usage of # define, you can come to our discussion
Use of # define in C language (GO)