From: http://blog.21ic.com/user1/349/archives/2008/48018.html
It is recommended to write C language. A beautiful macro definition is very important. Using a macro definition can prevent errors, improve portability, readability, and convenience. The following lists some mature software commonly used macro definitions ......
1. prevent a header file from being repeatedly contained
# Ifndef comdef_h
# Define comdef_h
// Header file content
# Endif
2. redefine some types to prevent the differences in the number of bytes of the types caused by different platforms and compilers, so as to facilitate migration.
Typedef unsigned char Boolean;/* Boolean value type .*/
Typedef unsigned long int uint32;/* unsigned 32 bit value */
Typedef unsigned short uint16;/* unsigned 16 bit value */
Typedef unsigned char uint8;/* unsigned 8 bit value */
Typedef signed long int int32;/* signed 32 bit value */
Typedef signed short int16;/* signed 16 bit value */
Typedef signed Char int8;/* signed 8 bit value */
// The following is not recommended
Typedef unsigned char byte;/* unsigned 8 bit value type .*/
Typedef unsigned short word;/* unsinged 16 bit value type .*/
Typedef unsigned long DWORD;/* unsigned 32 bit value type .*/
Typedef unsigned char uint1;/* unsigned 8 bit value type .*/
Typedef unsigned short uint2;/* unsigned 16 bit value type .*/
Typedef unsigned long uint4;/* unsigned 32 bit value type .*/
Typedef signed Char int1;/* signed 8 bit value type .*/
Typedef signed short int2;/* signed 16 bit value type .*/
Typedef long int int4;/* signed 32 bit value type .*/
Typedef signed long sint31;/* signed 32 bit value */
Typedef signed short sint15;/* signed 16 bit value */
Typedef signed Char sint7;/* signed 8 bit value */
3. Get a byte or word on the specified address.
# Define mem_ B (x) (* (byte *) (x )))
# Define mem_w (x) (* (word *) (X )))
4. Calculate the maximum and minimum values.
# Define max (x, y) (x)> (y ))? (X): (y ))
# Define min (x, y) (x) <(y ))? (X): (y ))
5. Get the offset of a field in the struct (struct ).
# Define FPOs (type, field )"
/* Lint-e545 */(DWORD) & (type *) 0)-> Field)/* lint + e545 */
6. Obtain the number of bytes occupied by the field in the struct.
# Define fsiz (type, field) sizeof (type *) 0)-> field)
7. convert two bytes into a word in LSB format
# Define flipw (Ray) (Word) (Ray) [0]) * 256) + (Ray) [1])
8. convert a word into two bytes in LSB format.
# Define flopw (Ray, Val )"
(Ray) [0] = (VAL)/256 );"
(Ray) [1] = (VAL) & 0xff)
9. Get the address of a variable (word width)
# Define B _ptr (VAR) (byte *) (void *) & (VAR ))
# Define w_ptr (VAR) (word *) (void *) & (VAR ))
10 to 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 ))
11. Return a multiple of 8 nearest to X.
# Define rnd8 (x) + 7)/8) * 8)
12. convert a letter into uppercase letters.
# Define upcase (C) (c)> = 'A' & (c) <= 'Z ')? (C)-0x20): (c ))
13. determines whether the character is a decimal number.
# Define decchk (C) (c)> = '0' & (c) <= '9 ')
14. determines whether the character is a hexadecimal number.
# Define hexchk (C) (c)> = '0' & (c) <= '9') |"
(C)> = 'A' & (c) <= 'F') |"
(C)> = 'A' & (c) <= 'F '))
15. A method to prevent overflow
# Define inc_sat (VAL) (val = (VAL) + 1> (VAL ))? (VAL) + 1: (VAL ))
16. returns the number of array elements.
# Define arr_size (A) (sizeof (a)/sizeof (A [0])
17. Return the value mod_by_power_of_two (x, n) = x % (2 ^ N) at the end of N)
# Define mod_by_power_of_two (Val, mod_by )"
(DWORD) (VAL) & (DWORD) (mod_by)-1 ))
18. For the structure of Io space ing in the bucket, Input/Output Processing
# Define Indium (port) (* (volatile byte *) (port )))
# Define inpw (port) (* (volatile word *) (port )))
# Define inpdw (port) (* (volatile DWORD *) (port )))
# Define outp (port, Val) (* (volatile byte *) (port) = (byte) (VAL )))
# Define outpw (port, Val) (* (volatile word *) (port) = (Word) (VAL )))
# Define outpdw (port, Val) (* (volatile DWORD *) (port) = (DWORD) (VAL )))
[Add]
19. Use some macro tracking for debugging
The a n s I Standard specifies five predefined macro names. They are:
_ L I n e _
_ F I L E _
_ D a t e _
_ T I m e _
_ S t d c _
If compilation is not standard, only a few of the above macro names are supported or not supported at all. Remember to compileProgram
Other predefined macro names may also be provided.
The _ L I n e _ and _ f I l e _ macro commands have been discussed in the section about # l I n e. The remaining macro names are discussed here.
The _ d at E _ macro command contains a string in the form of month/day/year, indicating that the source file is translatedCode.
Source codeThe time when the target code is translated as a string containing _ t I m e. String format: minute: second.
If the implementation is standard, the macro _ s t d c _ contains the decimal constant 1. If it contains any other number, the implementation is
Non-standard.
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
20. macro definition to prevent usage from being incorrect
Enclosed in parentheses.
Example: # define add (a, B) (a + B)
Use the do {} while (0) statement to include multiple statements to prevent errors.
Example: # difne do (A, B) A + B ;"
A ++;