C and C++ macros define of define

  • 2020-05-17 05:57:00
  • OfStack

# define C is provided in the macro definition command language, its main purpose is to provide 1 set for programmers in the programming is convenient, and can be in 1 degree, improving the efficiency of the program, but students in the learning often can't understand the nature of the command, always 1 herein produces some confusion, the misuse of this command when programming, makes the program runs with the expected purpose is not 1, others write program, or reading the run results wrong, this is adverse to C language learning.

The definition of a macro is very useful in a program, but if it is not used properly, it can cause a lot of trouble. This is often confused by the use of macros in computing.

This example is mainly about macro computation. Many times, you know how useful it is for compiling and programming to define a macro for computation. You now have the following macro defined to calculate "multiplication".


#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;  
}
</stdio.h>

For example, the variable i=10 in the program. In this case, the data obtained by calling the macro is as follows:

However, if the value of a variable is added or subtracted from itself, the result will be different.

Let's say we change the top program to something like this


#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;  
}
</stdio.h>

So instead of getting 11 times 11 times 11 is 1331, you get 1872, which is why?

Those of you who have macros or who have learned about macros in computing will know that it's not just a macro problem, it's a programmer problem writing this code. When ++i and i++ are used,

Note in particular that macros are all ++i or i++, and the format is as follows


MUL ( i++) ((i++)*(i++)*(i++))
 MUL(++i) ((++i)*(++i)*(++i))

The above method is obviously not the calculation result we want, and we may see MUL(++i) or MUL(i++) in our program, which is actually the following:

// when the initial value of i is 10, i++ MUL(i++) macro calculation is performed, that is, int i = 10;
// the value of MUL(i ++) is 10 * 11 * 12, which is no problem, but what about the value of i?? Is it eleven? Apparently not. MUL(i++) = 10 * 11 *12; i = & # 63; The & # 63;;

The value of i is shown in the figure below

Sure,, the i number has changed to 13. Why?

That's because of the MUL(a) macro and the programmer's "add and subtract" operations. Here, first popularize the "add and subtract" operation of C/C++ language:

// add and subtract operation

i + + and + + i - > The operation here is a ++ operation and can be replaced with the result that 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, and i is 11

The value of sum2 in 2 is 11, and i is 11

This is because:

The i++ operation is performed by i = i+1 after the initial assignment to sum1

The ++i operation is performed first with i = i+ 1, and then assigned to sum2

The result is different, of course, but the end result of i is going to be plus 1, but it's going to be different when you assign it to a variable

By adding and subtracting operations, I don't know if you understand why?

When i = 10, MUL(i++) is the calculation result of (i++)*(i++)*(i++). Considering the operator associability of C/C++,

First, calculate the first i++, which is the self-addition method of the first one after calculation, then assign the value, then this is the value of the first one (i++) to be determined to be 10, so the first

The i of 2 is changed due to the effect of the first data (i++). In this case, the value of the second data (i++) is 11

According to the associativity, the first two data are calculated as (i++) * (i++), which is: 10 * 11, while the i value is 12;

And then you take the third i plus plus, so i in the third i plus plus is 12, and then you add 1, so after 10 times 11 times 12,

The value of i= 12 is changing from i++ to 13. So MUL(i++) = 10 times 11 times 12 = 1320.

In addition, the operation of ++i is similar to the above situation, except that the operation of self-addition is done first, and the assignment is carried out.

When i = 10, MUL(++i) is actually (++i)*(++i)*(++i), so let's take the first one (++i), which is 1

Then, i = i+1 = 11; Now, when you're ready to compute the second (++i), because you have to compute it first and then assign it,

So the value after the second plus plus i is 12, but since i is a member of the same variable and property, then the first i is also going to be 12, which is associativity

It should be calculated with the first two (++i), and then with the third (++i), so (++i)*(++i) = 12 * 12; And then, let's calculate the third one

The value of (++i), because of the i value of the first 2 ++i, the third ++i is 13, at this point, 12 * 12 * 13.

One might wonder, why didn't it end up being 13 * 13 * 13? Isn't that always 13 at the end? -- actually, this idea is wrong.

You must first understand the associativity of the operators. And we know that when we have parentheses in our calculations, we're going to calculate what the parentheses are, and that's what we're used to thinking in mathematics. But for a computer, the computer must have the priority of the computation, that is, the priority problem of the operator. First, we calculate the contents of the first two parentheses, assuming that there is a multiplication sign (*) between the two parentheses, so after computing the first two parentheses (++i), we have to do the multiplication calculation, which is the multiplication calculation in the priority, from left to right. So it's going to be 12 times 12 and the final result is going to be 144 times (++i) with the third parenthesis, which is 144 times (++ i) = 144 times 13;

So the results of MUL(++i) are as follows:

Conclusion:

Use macros with caution in terms of computation, but macros are a bit too much. For the C language, macros can reduce running time. In C++, it is recommended to use const because the macro does not check the type and is not secure enough

Is used so that type 1 is guaranteed. This is the result of C/C++ tuning macro rigor.


Related articles: