========================================================== ==============================
Three special symbols in define :#,##,#@
========================================================== ==============================
# Define Conn (x, y) x # y
# Define ToChar (x) # @ x
# Define ToString (x) # x
(1) What does x # y represent? X connects to y, for example:
Int n = Conn (123,456);/* The result is n = 123456 ;*/
Char * str = Conn ("asdf", "adf");/* The result is str = "asdfadf ";*/
(2) Let's take a look at # @ x. In fact, a single quotation mark is added to x, and the returned result is a const char. For example:
Char a = ToChar (1); the result is a = '1 ';
Make an out-of-bounds test char a = ToChar (123); the result is wrong;
However, if your parameter contains more than four characters, the compiler reports an error!
Error C2015: too character characters in constant: P
(3) let's take a look at # x. You may also understand that it adds double quotation marks to x.
Char * str = ToString (123132); then str = "123132 ";
========================================================== ==============================
Some commonly used macro definitions
========================================================== ==============================
1. prevent a header file from being repeatedly contained
# Ifndef BODYDEF_H
# Define BODYDEF_H
// Header file content
# Endif
2. Get a byte or word on the specified address.
# Define MEM_ B (x) (* (byte *) (x )))
# Define MEM_W (x) (* (word *) (x )))
The usage is as follows:
# Include <iostream>
# Include <windows. h>
# Define MEM_ B (x) (* (byte *) (x )))
# Define MEM_W (x) (* (WORD *) (x )))
Int main ()
{
Int bTest = 0x123456;
Byte m = MEM_ B (& bTest);/* m = 0x56 */
Int n = MEM_W (& bTest);/* n = 0x3456 */
Return 0;
}
3. Get the offset of a field in the struct.
# Define OFFSETOF (type, field) (size_t) & (type *) 0)-> field)
4. Obtain the number of bytes occupied by the field in the struct.
# Define FSIZ (type, field) sizeof (type *) 0)-> field)
5. Get the address of a variable (word width)
# Define B _PTR (var) (byte *) (void *) & (var ))
# Define W_PTR (var) (word *) (void *) & (var ))
6. convert a letter into uppercase letters.
# Define UPCASE (c)> = ''a' & (c) <= ''z '')? (C)-0x20): (c ))
7. determines whether the character is a decimal number.
# Define DECCHK (c)> = '0' & (c) <= '9 '')
8. determines whether the character is a hexadecimal number.
# Define HEXCHK (c)> = '0' & (c) <= '9') | (c)> = ''a' & (c) <= ''f'') | (c)> = ''a'' & (c) <= ''f ''))
9. A method to prevent overflow
# Define INC_SAT (val) (val = (val) + 1> (val ))? (Val) + 1: (val ))
10. Return the number of array elements.
# Define ARR_SIZE (a) (sizeof (a)/sizeof (a [0])
11 use some macro tracking for debugging
During debugging, we can set the _ DEBUG macro or use the-D compilation option in Makefile,
[Cpp]
# Define _ DEBUG
# How to Use define _ DEBUG: [cpp] view plaincopyprint?
# Ifdef _ DEBUG
Printf ("% s ",...);
# Endif
# Ifdef _ DEBUG
Printf ("% s ",...);
# Endif In addition, there are several standard predefined Macros in the ansi c standard. The first few are commonly used in printf (sprintf) and other statements:
_ LINE __: Insert the current source code LINE number into the source code;
_ FILE __: insert the name of the current source FILE into the source FILE;
_ DATE __: Insert the current compilation DATE into the source file
_ TIME __: Insert the current compilation TIME into the source file;
_ STDC __: when the program strictly follows the ansi c standard, the ID is assigned 1;
_ Cplusplus: This identifier is defined when a C ++ program is compiled.
_ Cplusplus is commonly used in header files. The format is as follows:
[Cpp]
# Ifndef _ ZX_FUNC_H
# Define _ ZX_FUNC_H
# Ifdef _ cplusplus
Extern "C "{
# Endif
/* Functions */
Char * strdup (const char * s );
# Ifdef _ cplusplus
}
# Endif
# Endif
# Ifndef _ ZX_FUNC_H
# Define _ ZX_FUNC_H
# Ifdef _ cplusplus
Extern "C "{
# Endif
/* Functions */
Char * strdup (const char * s );
# Ifdef _ cplusplus
}
# Endif
# Endif
Extern "C" indicates that the code is compiled according to the C compilation method, with the aim of Calling C ++ and C language.
The difference between C compilation and C ++ compilation is that C will compile strdup into the _ STRDUP symbol, and C ++ will compile it into _ STRDUP_CHAR, this is why C ++ can implement function overloading. Extern can only appear in the C ++ file. Generally, the above method is placed in the header file.
To call C ++ code in C, you need to declare the function or variable in C code as the extern type, and modify the function or variable with extern "C" in C ++.
12 simple mathematical computation (absolute value, trigonometric function, etc)
[Cpp]
# Define ABS (a)> 0 )? (A): (-()))
# Define ABS (a)> 0 )? (A): (-()))
13 # define a complex statement
For example, exchanging values of a and B,
[Cpp]
# Define (a, B) do {\
Int t = 0;
T = ;\
A = B ;\
B = t ;\
} While (0)
# Define (a, B) do {\
Int t = 0;
T = ;\
A = B ;\
B = t ;\
} While (0)
Note: # advanced usage of define appears in many Linux source codes. For more information, see Linux source code.