Use macros with caution in C/C ++ (# define ),
Macro definition is very useful in the program, but improper use will cause a lot of trouble to itself. This problem is usually caused by the use of Macros in computing.
This example is mainly used in macro computing. In many cases, we all know how useful it is to define a computing macro for compilation and programming. Now we define a macro to calculate "multiplication" as the next one.
#include <stdio.h>#define MUL(a) ((a)*(a)*(a))int main(int argc,char *argv[]){ int i = 10; int sum = MUL(i); printf("MUL(%d) = %d\n",i,sum); return 0; }
This method of the above program is no problem for non-negative numbers. For example, the variable I = 10 in the program. At this time, the data obtained by calling the macro is as follows:
However, if the value of a variable is auto-increment or auto-increment, the result will be different.
If we change the above program to the following
#include <stdio.h>#define MUL(a) ((a)*(a)*(a))int main(int argc,char *argv[]){ int i = 10; int sum = MUL(++i); printf("MUL(%d) = %d\n",i,sum); return 0; }
The result is not 11*11*11 = 1331, but 1872. Why?
If you get a macro, or if you know about macro computing, you will know that this is not only a macro problem, but also a programmer's issue of writing this code. When ++ I and I ++ are used,
Pay special attention to the usage of ++ I or I ++ in macros. The format is as follows:
MUL(i++) ((i++)*(i++)*(i++))MUL(++i) ((++i)*(++i)*(++i))
The above is obviously not the expected calculation result. We may see MUL (++ I) or MUL (I ++) in our program. We think it is actually the following situation:
// When the initialization value of I is 10, perform MUL (I ++) macro computation of I ++, that is:
Int I = 10;
// The MUL (I ++) numeric calculation result is 10*11*12, which is no problem, but what about the I value ?? Is it 11 ?? Apparently not.
MUL (I ++) = 10*11*12;
I = ??;
Shows the I value.
It is true that the value of I is changed to 13. Why ??
That is because of the MUL (a) macro and the programmer's "auto-incrementing" operation. Here we will first popularize the "auto-increment and auto-increment" operations in C/C ++:
// Operation I ++ and ++ I ----> the operation here belongs to the operation after ++ and can be replaced with the result of I = I + 1. However, when it is assigned to a variable, the content and meaning are different: (Suppose I = 10) 1. sum1 = I ++; 2. sum2 = ++ I; the value of sum1 in 1 is 10, I is the value of sum2 in 11 2 is 11, and I is 11 because: the I ++ operation is first assigned to sum1, and then you are performing the I = I + 1 operation before performing the I = I + 1 operation, the result obtained by assigning values to sum2 is certainly different, but the final result of I is to add 1, but it is different when assigning values to variables.
By explaining the auto-increment and auto-increment operations, do you know why ??
When I = 10, MUL (I ++) is the calculation result of (I ++) * (I ++, considering the combination of C/C ++ operators, calculate the first I ++ first. This is an auto-increment method that calculates and assigns values first, so this is the first (I ++) the value of is to be determined as 10, so the second I is changed because the first data (I ++) plays a role. At this time, the second (I ++) the value is 11, and then 1 is added. At this time, according to the combination, the first two data values are calculated, that is, the values of (I ++) * (I ++), that is: 10*11. At this time, the I value is 12. Then, the value of the third I ++ is calculated. At this time, the I value in the third I ++ is 12. After calculation, add 1, that is to say, after 10*11*12, the value of I = 12 is changed to 13 after I ++. Therefore, MUL (I ++) = 10*11*12 = 1320.
In addition, the operations on ++ I are similar to those described above, except that the self-increment operation is performed first and the value is assigned.
When I = 10, MUL (++ I) is actually (++ I) * (++ I, at this time, calculate the first (++ I) first. This is a combination of First calculation and then value assignment, so I = I + 1 = 11. At this time, we prepare to calculate the second (++ I) in this case, the value after the second ++ I is 12 because I belongs to the same variable and attribute, then the first I will change to 12. At this time, the combination consideration should be to calculate the results of the first two (++ I) and then calculate with the third (++ I, that is, (++ I) * (++ I) = 12*12; then, we calculate the third (++ I) value, because of the I value of the second ++ I, the third ++ I is 13. At this time, 12*12*13. Some people may worry, why is it not the last 13*13*13? Isn't it all 13 in the end ?? ------ In fact, this idea is wrong. We must first understand the combination of operators. We know that when we encounter parentheses in calculation, we first calculate the content of the brackets. This is our inertial thinking in mathematics. However, for a computer, the computer must have a computing priority, that is, the operator priority. First, we calculate the content of the first two parentheses to assume that there is a multiplication sign (*) between the two parentheses. Therefore, After calculating the first two (++ I), we must perform multiplication calculation, this is the multiplication calculation in the priority, which is calculated from left to right. Therefore, the final result changed to 12*12 is calculated with (++ I) in the third bracket, that is, 144 * (++ I) = 144*13;
The MUL (++ I) result is as follows:
Summary:
Exercise caution when using macros for computing, but there are still a lot of macros. For the C language, macros can reduce the running time. In C ++, we recommend that you use const
To ensure that the types are consistent. This is the result of C/C ++'s strict macro optimization. For more macro knowledge or how to define macros, you can check the information online.