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 different platforms from compilers, the difference in the number of bytes of the type is convenient for transplantation. 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 It is not recommended to use 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 ch Ar 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 to obtain a byte or word on the specified address # define mem_ B (x) (* (byte *) (X ))) # define mem_w (x) (* (WO Rd *) (x) 4, calculate the maximum and minimum # 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 to 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 # define flipw (Ray) (Word) (Ray) according to the LSB format) [0]) * 256) + (Ray) [1]) 8. convert a word into two bytes in LSB format # define flopw (Ray, Val) \ (Ray) [0] = (VAL)/256); \ (Ray) [1] = (VA L) & 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, get the high and low byte of a word # define word_lo (XXX) (byte) (Word) (XXX) & 255) # define word_hi (XXX) (byte) (Word) (XXX)> 8) 11, returns a multiple of 8 nearest to X # define rnd8 (x) + 7)/8) * 8) 12, convert a letter to uppercase # define upcase (C) (c)> = 'A' & (c) <= 'z '')? (C)-0x20): (c) 13, judge whether the character is a number with a value of 10 # define decchk (C) (c)> = '0' & (c) <= '9') 14. Determine 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 ()) /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 IO space ing structure in the bucket, input/Output Processing # define Indium (port) (* (volatile byte *) (port) # define inpw (port) (* (volatile word *) (port) # define inpdw (port) (* (volatile DWORD *) (port) # defi Ne 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) 19. Use some macro tracking to debug the n s I standard to illustrate the 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 the compilation is not standard, only a few of the above macro names are supported, or they are not supported at all. Remember to compile Program 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 translated Code . Source code The 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 a macro. For example, when _ debug is defined, the output data and the row where the file is located # ifdef _ debug # define debugmsg (MSG, date) printf (MSG ); printf ("% d", date, _ line _, _ file _) # else # define debugmsg (MSG, date) # endif 20, the macro definition prevents the use from being incorrectly included in parentheses. For example: # define add (a, B) (a + B) Use the do {} while (0) statement to include multiple statements to prevent errors. For example: # difne do (A, B) A + B; \ A ++; application time: if (....) Do (a, B); // error else solution: # difne do (a, B) do {A + B; \ A ++;} while (0) usage of "#" and "#" in macro I. general usage we use # To convert macro parameters into a string, and use # to combine the two macro parameters. usage: # include <cstdio> # include <climits> using namespace STD; # define STR (s) # s # define cons (A, B) INT (A ## e ## B) int main () {printf (STR (vck); // output string "vck" printf ("% d \ n ", cons (2, 3); // 2e3 output: 2000 return 0 ;} 2. When the macro parameter is another macro, it should be noted that the local macro parameter ''#'' or ''#'' in the macro definition will not be expanded. 1. Non-''#'' and ''# define tow (2) # define MUL (a, B) (a * B) printf ("% d * % d = % d \ n", tow, tow, MUL (tow, tow); The Macros in this line are expanded: printf ("% d * % d = % d \ n", (2), (2), (2) * (2 ))); the tow parameter in Mul is expanded to (2 ). 2. When there is ''#'' or ''#'', # define a (2) # define STR (s) # s # define cons (A, B) INT (A # e # B) printf ("int MAX: % s \ n", STR (int_max )); // int_max # include <climits> This row is expanded to: printf ("int MAX: % s \ n", "int_max"); printf ("% s \ n ", cons (A, A); // The compile error line is: printf ("% s \ n", INT (AEA); int_max and a will not be expanded, however, the solution to this problem is simple. add another intermediate conversion macro. the purpose of adding this macro layer is to expand all macro parameters in this layer, so that the macro (_ Str) in the conversion macro can get the correct macro parameters. # define a (2) # DEFINE _ STR (s) # s # define STR (s) _ STR (s) // convert macro # DEFINE _ cons (A, B) INT (A ## e ## B) # define cons (a, B) _ cons (a, B) // convert the macro printf ("int Max: % s \ n ", STR (int_max); // The maximum value of int_max, int type. It is a variable # include <climits> and the output is int MAX: 0x7fffffff STR (int_max) --> _ STR (0x7fffffff) and then convert it to a string; printf ("% d \ n", cons (A, A); output: 200 cons (A,) --> _ cons (2), (2) --> int (2) E (2 )) iii. Application exceptions of ''#'' and ''#'' 1. Merge anonymous variable names # define ___ anonymous1 (type, VAR, line) type var # line # DEFINE _ anonymous0 (type, line) ___ anonymous1 (type, _ anonymous, line) # define anonymous (type) _ anonymous0 (type, _ line _) Example: anonymous (static INT); that is, static int _ anonymous70; 70 indicates the row number; Level 1: anonymous (static INT ); --> _ anonymous0 (static int, _ line _); Layer 2: --> ___ anonymous1 (static int, _ anonymous, 70); Layer 3: --> static int _ anonymous70; that is, only macros of the current layer can be unlocked at each time. Therefore, _ line _ can be unlocked at the second layer. 2. Fill in the structure # define fill () {A, # A} Enum IDD {open, close}; typedef struct MSG {idd id; const char * MSG;} MSG; MSG _ MSG [] = {fill (open), fill (close)}; equivalent to: MSG _ MSG [] = {open, "open"}, {close, "Close" }}; 3. Record File name # DEFINE _ get_file_name (f) # F # define get_file_name (f) _ get_file_name (f) static char file_name [] = get_file_name (_ file _); 4. Obtain the buffer size of the string corresponding to the value type # DEFINE _ type_buf_size (type) sizeof # type # define type_buf_size (type) _ type_buf_size (type) Char Buf [type_buf_size (int_max)]; --> char Buf [_ type_buf_size (0x7fffffff)]; --> char Buf [sizeof "0x7fffffff"]; equivalent to: Char Buf [11];
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.