The definition of a macro is very useful in a program, but using it incorrectly can cause a lot of trouble. Usually this is the problem: macros are used in terms of computing.
This example is mainly in the calculation of macros, many times, we all know how to define a calculated macro, for the compilation and programming is how useful. Now you have one of the following macros that calculate "multiplication".
#include <stdio.h> #define MUL (a) ((a) * (a) * (a)) int main (int argc,char *argv[]) { int i = ten; int sum = MUL (i); printf ("MUL (%d) =%d\n", i,sum); return 0; }
This practice of the above program for non-negative is that there is no problem, for example, the variable i=10 in the program, this time, the call macro to get the following data:
But how the value of a variable is self-adding or self-reducing, the result is different.
If we turn the above program into the following
#include <stdio.h> #define MUL (a) ((a) * (a) * (a)) int main (int argc,char *argv[]) { int i = ten; int sum = MUL (++i); printf ("MUL (%d) =%d\n", i,sum); return 0; }
The result is not 11 * 11 *11 = 1331 this data, but 1872, this time someone will ask why?
A friend who gets a macro or knows about a macro in computing will know that it's a matter of macro, or that the programmer is writing the code itself. When ++i and i++ are used,
Pay special attention to the use of ++i or i++ in the macro, and the format is as follows
MUL (i++) ((i++) * (i++) * (i++)) MUL (++i) ((++i) * (++i) * (++i))
The above-mentioned practice is obviously not the result of the calculation we want, it may be seen in our program is MUL (++i) or MUL (i++), that is actually the following situation:
When the initialization value of I is 10, the i++ MUL (i++) macro is calculated, which is:
int i = 10;
MUL (i + +) numerical results compared to 10 * 11 * 12, this is not a problem, but the value of I?? Is it 11?? Obviously not.
MUL (i++) = 10 * 11 *12;
i =??;
The value of I is as shown
True, the value of I becomes 13, which is why??
That's because of this mul (a) macro and the programmer's "self-add-subtract" operation. This is where the "self-add-subtract" operation of the C + + language is first popularized:
Self-decrement operation i++ and ++i ----> here the operation belongs to + + after operation, can replace the result of I = i+1. However, when it is assigned to a variable, the content and meaning of the expression are different: (assuming i = ten) 1. sum1 = i++; 2. sum2 = ++i; The value of the SUM1 in 1 is ten, I is 2 of the value of the sum2 is one, I for each of this is because: i++ operation is first assigned to SUM1, the operation i = i+1 the ++i operation is performed first i = i+ 1 of the operation, and then assigned to sum2 so the result of course is different, but I the final result is to add 1, but is assigned to the variable when there will be different
By the self-added self-reduction operation to explain, do not know whether people understand why it?
When I = 10, MUL (i++) is the calculation result of (i++) * (i++) * (i++), considering the operator binding of C/s + +, first i++ is calculated first, this is a self-added method of first calculation, then this is the first one (i++) The value is determined to be 10, then the second I is because the first data (i++) has a role in the change, this time the second (i++) value is 11, and then add 1, this time according to the binding, the first two data is calculated, that is (i++) * (i++) value, that is: 10 * 11, The value of I at this time is 12, then the value of the third i++ is calculated, when the number of I in the third i++ is 12, the calculation is added 1, that is, 10 * 11 * 12, the value of i= 12 is i++ into 13. So MUL (i++) = 10 * 11 * 12 = 1320.
In addition, the operation of the ++i is similar to the above, except that it is the first to do the self-added operation, in the assignment.
When i = 10, MUL (++i) is actually (++i) * (++i) * (++i) way, this time first calculation (++i), this is a calculation after the assignment of the combination, then i = i+1 = 11; When we are ready to calculate the second (++i), Since the second ++i has a value of 12 after the first calculation, but because I belongs to the same variable and attribute, the first I also becomes 12, when the binding consideration should be the result of calculating the first two (++i), and then the third (++i) calculation, i.e. (++i) * (++i) = 12 * 12; Then, we calculate the third (++i) value, because the first second ++i of the I values, so the third ++i is 13, at this time, 12 * 12 * 13. Some may be concerned, why is not the last 13 * 13 * 13? Isn't that the last 13??
so the results of Mul (++i) are as follows:
Summarize:
Use the macro in the calculation, but the macro is a bit more, for the C language, the macro can reduce the running time. In C + +, macros are not secure enough because they do not check for types, so it is recommended that you use const to
To ensure that the type is consistent. This is the result of an optimization of the macro rigor of the C/s + +. More macro knowledge or how to define a macro, you can check the information on the Internet.
Use Macro (#define) with caution in C + +