# Define usage
1.Simple define Definition
# Define maxtime1000
A simple maxtime is defined. It represents 1000. If you write
If (I <maxtime ){.........}
The compiler replaces maxtime with 1000 before processing this code.
This definition looks similar to a normal constant definition const, but it is also different, because the definition of define is more like a simple text replacement, rather than being used as a volume, this problem is particularly highlighted below.
2. Define"Function Definition"
Define can accept some parameters as the function does, as follows:
# Define max (x, y) (x)> (y )? (X) :( y );
This definition will return the big one in two numbers. Have you seen it? Because this "function" does not have a type check, it is like a function template. Of course, it is definitely not as secure as a template. It can be used as a simple template.
However, there are hidden risks. The example is as follows:
# Define add (A, B) A + B;
In general use, there is no problem, but if you encounter problems such as: C * Add (a, B) * D, the purpose of the algebraic formula is that a + B is then multiplied by C and D, but because define is used (it is just a simple replacement), the formula is actually changed
C * A + B * d
Another example is as follows:
# Define pin (int *);
Pin A, B;
The intention is that A and B are both int-type pointers, but they actually become int * a, B;
A is an int pointer while B is an int variable.
This should use typedef instead of define, so that both A and B are int type pointers.
Therefore, when defining it, we should develop a good habit. We recommend that you add brackets to all layers.
3.Macro single row Definition
# Define a (x) T _ # x
# Define B (X) # @ x
# Define C (x) # x
Suppose: x = 1, there are:
A (1) ------> T_1
B (1) ------> '1'
C (1) ------> "1"
(For more information, see hustli)
4. DefineMulti-line definition
Define can replace multiple lines of code, such as the macro definition in MFC (very classic, although disgusting)
# Define macro (arg1, arg2) do {\
/* Declarations */\
Stmt1 ;\
Stmt2 ;\
/*...*/\
} While (0)/* (no trailing ;)*/
The key is to add "\" to each line feed "\"
Excerpted from the http://www.blog.edu.cn/user1/16293/archives/2005/115370.shtml fix several bugs
5.In large-scale development, especially for cross-platform and system software, Conditional compilation is the most important function of define.
That is:
# Ifdef windows
......
......
# Endif
# Ifdef Linux
......
......
# Endif
You can use # define to set the compiling environment during compilation.
6.How to define and cancel macros
// Definition macro
# Define [macroname] [macrovalue]
// Cancel the macro
# UNDEF [macroname]
Normal macro
# Define Pi (3.1415926)
Macro with Parameters
# Define max (A, B) (a)> (B )? (A), (B ))
The key is that errors are easily generated, including differences in Machine understanding and human understanding.
7. 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
8. The header file (. h) can be included by the header file or the c file;
Duplicate include (repeated definition)
Because header files can be nested, the C file may contain the same header file multiple times, and the definition may be repeated.