1.SimpleDefineDefinition
# Define maxtime1000
2. DefineOf"Function Definition"
Define can accept some parameters as the function does, as follows:
# Define max (x, y) (x)> (y )? (X) :( y );
Because this "function" does not have a type check, it is like a function template. It is safe without a template.
However, there are hidden risks. The example is as follows:
# Define add (A, B) A + B; if it encounters C * Add (a, B) * D, the problem may occur.
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.
In this case, typedef should be used instead of define, so that both A and B are int pointers.
When defining it, we develop a good habit. We recommend that you add brackets to all layers.
3.Single Row definition of macros (rarely seen)
# 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"
It can be seen that # Is a simple connector,
# @ Is used to add single quotes to parameters and convert them into single characters (the last character)
# It is used to convert parameters into strings by adding double quotation marks
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 "\"
5.In large-scale development, especially cross-platform and system software,DefineThe most important feature is Conditional compilation.
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
# Define [macroname] [macrovalue] // fixed macro
# UNDEF [macroname] // cancel the macro
# Define Pi (3.1415926) // normal macro
# Define max (A, B) (a)> (B )? (A), (B) // macro with Parameters
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. Prevents Repeated inclusion of a header file
Because header files can be nested, the C file may contain the same header file multiple times, and the definition may be repeated.
Use the Conditional compilation switch to avoid repeated inclusion (repeated definition)
# Ifndef comdef_h
# Define comdef_h
// Header file content
# Endif
Because header files can be nested, the C file may contain the same header file multiple times, and the definition may be repeated. Use the Conditional compilation switch to avoid repeated inclusion (repeated definition ). Because # define comdef_h is not always defined when it compiles the first header file, it will compile all the content in the header file, including the definition # define comdef_h. In this way, if the header file to be compiled is encountered during the next compilation, the statement # ifndef comdef_h will not repeatedly compile this header file because of the existence of the statement # ifndef comdef_h.
1.Obtains a byte or word on the specified address.
# Define mem_ B (x) (* (byte *) (x )))
# Define mem_w (x) (* (word *) (X )))
2.Calculate the maximum and minimum values
# Define max (x, y) (x)> (y ))? (X): (y ))
# Define min (x, y) (x) <(y ))? (X): (y ))
3.GetFieldIn struct(Struct)Offset in
# Define FPOs (type, field) & (type *) 0)-> field)
4.Get a structFieldNumber of bytes occupied
# Define fsiz (type, field) sizeof (type *) 0)-> field)
5.FollowLSBFormat to convert two bytes into oneWord
# Define flipw (Ray) (Word) (Ray) [0]) * 256) + (Ray) [1])
6.FollowLSBFormatWordConvert to two bytes
# Define flopw (Ray, Val )\
(Ray) [0] = (VAL)/256 );\
(Ray) [1] = (VAL) & 0xff)
7.Get the address of a variable (WordWidth)
# Define B _ptr (VAR) (byte *) (void *) & (VAR ))
# Define w_ptr (VAR) (word *) (void *) & (VAR ))
8.Obtain the high and low bytes of a word.
# Define word_lo (XXX) (byte) (Word) (XXX) & 255 ))
# Define word_hi (XXX) (byte) (Word) (XXX)> 8 ))
9.Returns a ratio.XThe largest closest8Multiples
# Define rnd8 (x) + 7)/8) * 8)
10.Converts a letter to uppercase.
# Define upcase (C) (c)> = 'A' & (c) <= 'Z ')? (C)-0x20): (c ))
11.Judge whether the character is10Number of the input value
# Define decchk (C) (c)> = '0' & (c) <= '9 ')
12.Judge whether the character is16Number of the input value
# Define hexchk (C) (c)> = '0' & (c) <= '9') | \
(C)> = 'A' & (c) <= 'F') | \
(C)> = 'A' & (c) <= 'F '))
13.A method to prevent overflow
# Define inc_sat (VAL) (val = (VAL) + 1> (VAL ))? (VAL) + 1: (VAL ))
14.Returns the number of array elements.
# Define arr_size (A) (sizeof (a)/sizeof (A [0]) <! -- [If! Supportannotations] --> [lst1] <! -- [Endif] -->
15.Returns an unsigned number.NEnd valueMod_by_power_of_two (x, n) = x % (2 ^ N)
# Define mod_by_power_of_two (Val, mod_by )\
(DWORD) (VAL) & (DWORD) (mod_by)-1 ))
16.ForIoSpace ing in the bucket structure, Input/Output Processing
# Define Indium (port) (* (volatile byte *) (port )))
# Define inpw (port) (* (volatile word *) (port )))
# Define outp (port, Val) (* (volatile byte *) (port) = (byte) (VAL )))
# Define outpw (port, Val) (* (volatile word *) (port) = (Word) (VAL )))
17.Debug with some macro tracking
The ANSI standard specifies five predefined macro names. They are:
_ Line __
_ File __
_ Date __
_ Time __
_ Stdc __
C ++ also defines _ cplusplus
If the compiler is not standard, only a few of the above macro names are supported or not supported at all. Remember that the Compilation Program may also provide other predefined macro names.
_ Line _ and _ file _ macro instructions. # The line command can change its value. In short, during compilation, it contains the current number of lines and file names of the program.
The _ date _ macro command contains a string in the form of month, day, or year, which indicates the date when the source file was translated to the code.
The _ time _ macro command contains the program Compilation Time. Time is represented by a string in the form of minute: Second
The significance of the _ stdc _ macro command is defined during compilation. Generally, if _ stdc _ has been defined, the compiler will only accept standard C/C ++ code that does not contain any non-standard extensions. If the implementation is standard, macro _ stdc _ contains the decimal constant 1. If it contains any other number, the implementation is non-standard.
The consistent compiler of _ cplusplus and Standard C ++ defines it as a value containing at least six characters. A compiler that is inconsistent with the standard C ++ will use a value of 5 or less.
You can define macros, for example:
When _ debug is defined, the output data and the row of the file
# Ifdef _ debug
# Define debugmsg (MSG, date) printf (MSG); printf ("% d", date, _ line _, _ file _)
# Else
# Define debugmsg (MSG, date)
# Endif
18.Macro definition to prevent the inclusion of braces by mistake.
For example:
Definition in question: # define dump_write (ADDR, NR) {memcpy (bufp, ADDR, NR); bufp + = nR ;}
Definition: # defne do (a, B) do {A + B; A ++;} while (0)
For example:
If (ADDR)
Dump_write (ADDR, NR );
Else
Do_somethong_else ();
This will happen after the macro scale:
If (ADDR)
{Memcpy (bufp, ADDR, NR); bufp + = nR ;};
Else
Do_something_else ();
When GCC encounters the ";" in front of Else, it considers that the if statement has ended, so the subsequent else is not in the IF statement. With the do {} while (0) definition, there is no problem under any circumstances. The definition of # define do (a, B) do {A + B; A ++;} while (0) will not go wrong under any circumstances.
19. DefineSpecial identifier in
# Define conn (x, y) x # Y
# Define tochar (x) # @ x
# Define tostring (x) # x
Int A = conn (12, 34 );
Char B = tochar ();
Char C [] = tostring ();
The result is a = 1234, B = 'A', c = "";
It can be seen that # Is a simple connector,
# @ Is used to add single quotes to parameters and convert them into single characters (the last character)
# It is used to convert parameters into strings by adding double quotation marks