[Let's talk briefly] The difference between macros and functions

Difference between macro and function

  When we gradually understand the role of macros and functions by learning C language, we inevitably have such doubts: What is the difference between macros and functions? Why do some codes use macros to implement functions, while others use functions?
  Here I summarize the difference between them based on what I have learned. I hope that the friends who see this article can understand the difference between them, and they can complete the code editing perfectly without bugs in the process of future use.

Where are macros used?

  Judging from the codes we have seen so far, there are many places where macros are used, such as the three macros va_start, va_arg, and va_end that I mentioned in my [Introduction] Variable Parameter List. Why write them as macros instead of functions? Here I want to introduce one of the biggest differences between
  macro comparison functions , that is, the parameters of macros can pass parameters of any type, and even type names. As for the va_arg macro I mentioned in [Introduction] Variable parameter list:   the first parameter of this macro The two parameters passed is the type name of int, and if it is replaced by a function, there is no formal parameter that can receive this int type, right?   Next, I will introduce the second difference between macros and functions : sometimes the efficiency of using macros is higher than that of functions. If you can’t think of it directly, you might as well take a look at the following example:   Knowing the creation and destruction of stack frames, I believe everyone You should have a little impression of the assembly statement to be executed when the function is created, so everyone can think that when the function is created, it will not only create a certain space in memory, but also have a process of passing and returning parameters, and its efficiency must be It is much slower than macros (because macros are used to complete the replacement in the pre-compilation stage, and do not create extra space or pass extra parameters). So having said so many advantages of macros, can macros completely replace functions?   Of course the answer is  no
  write picture description here

  

  write picture description here       write picture description here

  
  
 

Possible side effects when passing parameters to macros

  If you are using macros without careful circumvention, you may use macro parameters with side effects, such as:

  i++

  If only this parameter has side effects, I believe everyone will be unconvinced. Here I give a piece of code, you can try it yourself to see if the result is what you expected, and why there is a situation that is different from the expected result ?

  #define MIN(A, B) (A < B ? A : B)  

    int a = 10;
    int b = 20;
    int ret = MIN(a++, b++);
    printf("%d %d %d\n", a, b, ret);

  And can this code get the expected result?

  #define COUNT(A, B) (A * B + B)  

    int a = 10;
    int b = 20;
    int ret = COUNT(a+b, a+b);
    printf("%d\n", ret);

Summarize

  
  For possible side effects of macros, it is recommended to add more parentheses to the parameters in the macro:

  #define COUNT(A, B) ((A) * (B) + (B))  

    int a = 10;
    int b = 20;
    int ret = COUNT(a+b, a+b);
    printf("%d\n", ret);

  Given the side effects of macro parameters, it is recommended to use macros with caution, or avoid them.
  

The full text is over, thank you for browsing

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325562576&siteId=291194637