Original: http://community.9cbs.net/expert/topic/3194/3194845.xml? Temp = .2763483
Start, why do you want to use Define?
We always need some code as follows when writing programs.
INT * p = (int *) malloc (sizeof (int) * 100)
For (i = 0; i <100; i ) {..............} Ok, this code can run normally, but one day, suddenly I feel that 100 is a little less. , I plan to apply for 1000 int, changed to the following code
INT * P = (int *) malloc (intend * 1000)
For (i = 0; i <1000; i ) {..............} In order to make this code work properly, change twice, if you use more places The maximum value of the array, then I may need to modify each place, I can't imagine horror, and even if I can modify 1000, I have not necessarily knew what this 1000 is what this 1000 is meant. For each programmer, it will be imagined. If there is something similar to Arraymax, it is better than 1000. There is an increase in the readability of the program and make the program easy to modify.
When writing a program, some small functions are always required, such as a function that returns two larger values, and it is well known that the function call will bring a relatively large system overhead, but we can't always write
INT D = x> y? x: y;
This statement, one is a lot of repetitive code, the other is that the program is difficult to maintain, if there is a mode, he looks like a function, but does not bring the system overhead like the function, it will make this The situation is greatly changed.
Let's first take a look at the basic syntax of Define, as follows:
1. Simple Define Definition
#define 1000 max
A simple max is defined, it represents 1000, if you write for For (i = 0; i
{
..............
}
The compiler is replaced with the MAX before processing this code.
2. DEFINE "function definition"
Define can accept some parameters as the function, as follows
#define max (x, y) (x)> (y)? (x): (Y);
This definition will return the bigger one of the two numbers. Have you seen it? Because this "function" has no type check, it is like a function template. Of course, it absolutely no template is so safe. It can be used as a simple template.
3. Macro single line definition
#define a (x) t _ ## x #define bx) # @ x #define cx) #x We assume: x = 1, there are: a (1) ------> T_1 b (1) - -----> '1' c (1) ------> "1"
(Here, refer to HuSTLI's article)
3. DEFINE