2

Summary

1) In the C language, the actual parameter and the function parameter are only value transfer, so the value of the actual parameter cannot be changed inside the function.

2) Function is a means of code reuse

3) Macro is the supplement code reuse in C language. It is a supplement to function; the relationship between function and macro is similar to the relationship between actors and avatars in life, but only used in some special scenes. In most cases, functions are still preferred.

4) The macro does not have the overhead of calling the , and the function call needs to maintain an activity record on the stack; the macro is just a simple text replacement, so the code block will expand, and the function will only use the same function body; the macro replacement will not perform the type Check, when macros and operators are combined, unexpected errors will occur. The function will perform type checking, and there will be no ambiguity;

5) Introduction to the composition of the compiler:

  1. preprocessing module: Process all the statements beginning with # (copy and paste replacement --> intermediate file.i)
  2. compilation module: Translate C program into binary program (intermediate file.i --> assembly file.s --> binary file.o)
  3. link module: Combine binary programs into executable programs (.o --> .out)

Macro definition

Question: What does the following code output? Why?

void swap(int a, int b)
{
    int t = a;
    a = b;
    b = t;
}

int main()
{
    int x = 1;
    int y = 2;
    
    swap(x, y);
    
    x = ?
    y = ?
}

The output of the above code is x = 1, y = 2; although we have exchanged the values of x and y, the reason is: is only a value transfer between the actual parameter and the formal parameter, and all functions cannot be directly changed The value of the actual parameter.

Understand the function again: function is a means of code reuse

  • Encapsulate the code piece that realizes a certain function (as a whole)
  • Give this code snippet a suitable name (use the code by name)
  • Define the parameters (define the problem that the code snippet needs to deal with)

The macro in the C language is the supplement to the function "defect"

  • Macro is a supplementary way of code reuse in C language
  • Syntax of macro definition: #define MACRO(param) code_segment
  • Macro usage syntax: MACRO(num);

    #define ADD(a, b) a + b
    
    int z = ADD(1, 2);

The difference between macros and functions:

  • Macro is not a function, there is no function call process (there is no need to maintain the record of function call on the stack); the function call will first pass the parameter value, then jump to execute the function body, and then return
  • use of macros is simply "code copy and paste", and then replace the parameters
  • The same function, no matter how many times it is called, there is only one function body code; the same macro, will "copy and paste" the same code every time

Introduction to the composition of the compiler:

  1. preprocessing module: process all the statements beginning with # (copy and paste replacement --> intermediate file.i)
  2. compilation module: Translate C program into binary program (intermediate file.i --> assembly file.s --> binary file.o)
  3. link module: Combine binary programs into executable programs (.o --> .out)

On the macro constant again: #define NAME value

  • The preprocessing module replaces all the text of the NAME identifier appearing in the code with value
  • Therefore, the macro constant is essentially the same as the literal (the real constant)

This article is summarized from "Introduction to C Language Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.


bryson
169 声望12 粉丝