English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C/C++Macro Definition (#define)

#define is a macro definition command provided by C language, whose main purpose is to provide certain convenience for programmers during programming and to improve the running efficiency of the program to a certain extent. However, students often cannot understand the essence of this command and always have some confusion here, misusing the command during programming, causing the program to run inconsistent with the expected goal, or misunderstanding the running results when reading others' programs, which is very不利 for the study of C language.

The definition of macros in the program is very useful, but improper use can cause great trouble to oneself. Usually, this kind of trouble occurs when macros are used in calculations.

This example mainly focuses on the calculation of macros, and many times, everyone knows that defining a calculation macro is very useful for compilation and programming. Now let's define a calculation "multiplication" macro.

#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>

  This approach of the above program is fine for non-negative numbers, for example, the variable i in the program =10At this time, the data obtained by calling the macro is as follows:

  However, if the variable is an increment or decrement operation, 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;  
}
</stdio.h>

  The result obtained is not 11 * 11 *11 = 1331This data, but 1872,this is when someone might ask why?

  Those who have received the macro or those who are familiar with macros in terms of calculation will know that this is not only a macro problem, but also a problem with the code written by the programmer. When using ++i and i++ when

It should be noted that in macros, the use of all ++i or i++It becomes a format as follows

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

  The approach mentioned above is clearly not the desired calculation result, and what we may see in our program is MUL(++i) 或者 MUL(i++i) or MUL(i

//) is considered to be as follows:10when i is initialized to++when, MUL(i++) macro calculation, namely: int i = 10;
//MUL(i ++) calculation result compared to 10 * 11 * 12is correct, but what about the value of i?? It is11?? Obviously not. MUL(i++) = 10 * 11 *12;i = ??;

  The value of i is as shown in the figure below

  Of course, the value of i has changed to 13, why is that??

  That is because of this MUL(a) macro and the programmer's 'increment and decrement' operation. Let's first popularize C/C++language's 'increment and decrement' operations:

//increment and decrement operations

i++ and ++i  ----> This operation belongs to++operations can be replaced with i = i+1 result.                   

    However, when it is assigned to a variable, the content and meaning are different: (assuming i = 10)

    1. sum1 = i++;

    2. sum2 = ++i; 

    1in1The value of is 10, i is 11

    2in2The value of is 11, i is 11

This is because:

    i++ The operation is to first assign to sum1operations+1The operation of i is to first perform i = i

    ++i+ 1operations, and then assigned to sum2 

The result obtained in this way is of course different, but the final result of i needs to be added1of course, the assignment to a variable is different

  By explaining the operations of increment and decrement, do you understand why??

when i = 10when, MUL(i++) is to (i++)*(i++)*(i++) results, considering C/C++The operation is the associativity of the operator, 

First calculate the first i++, this is a way of calculation before assignment with increment, so this is the first (i++) is to be determined 10 Then the value of 

The second i is because the first data's (i++) is affected by the operation, at this time, the second (i++) is the value of11, then add1, then 

According to the associativity, first calculate the first two data, that is (i++) * (i++) is the value, that is to say:10 * 11then, at this time, the value of i is 12; 

then calculate the third i++at this time, the third i++the value of i in 12, after calculation and then added1, that is to say,10 * 11 * 12after that,

i= 12 the value of i during++changes to 13so MUL(i++) = 10 * 11 * 12 = 1320.   

  Additionally, during++the operation of i is similar to the above, but it performs the increment operation first and then assigns the value.

when i = 10when, MUL(++i) actually is also (++i)*(++i)*(++the way of i), at this time, calculate the first (++), this is 

the first calculation before assignment, then i = i+1 = 11; at this time, prepare to calculate the second (++when 'i' is used, because it requires calculation before assignment, 

So the second ++i the value after i is12, but because i belongs to the same variable and attribute, then the first i will also become 12at this time, the associativity

Consider should be the calculation of the first two (++i) result, and then with the third (++i) calculation, that is (++i)*(++i) = 12 * 12; then, we calculate the third 

(++i) value, due to the second++i's i value, so the third++i is 13, at this time,12 * 12 * 13.  

  Someone may be worried, why is it not13 * 13 * 13how is it? Isn't it all13is that so ??  ------In fact, this idea is wrong,

This must first understand the associativity of the operator. We know that when we encounter parentheses in the calculation, we first calculate the content inside the parentheses, which is our habitual thinking in mathematics. However, for computers, computers must have calculation priority, that is, the priority of operators. First, we calculate the content inside the first two parentheses, because there is a multiplication sign (*) so the calculation of the first two (++i) must be multiplied, which is the multiplication calculation in the priority, calculated from left to right. Therefore, the result becomes 12 * 12The final result after the third parenthesis (++i) calculation is144 * (++ i) = 144 * 13;

   So MUL(++The result of i) is as follows:

Summary:

  Be cautious when using macros in calculations, but there are still many advantages to macros. For C language, macros can reduce running time.++In, macros do not check types, so they are not safe. Therefore, it is recommended to use const to

To use, this can ensure type consistency. This is C/C++The result of optimizing the rigor of macros.

The following is what the editor introduces to everyone about C/C++The macro (#define) defines knowledge, hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to you in time. I am also very grateful for everyone's support of the Yelling Tutorial website!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like