Zhu Youpeng Embedded Course - 4.6 macro definition and preprocessing, function and function library

1. Pretreatment

1.1 process from source code to executable program

Source code. c -- > preprocessing (preprocessor) - > compilation (compiler) - > assembly file. s -- > assembly (assembler) - > object file. o -- > link (linker) - > executable program.

The above "tools" together with other tools are called compiler tool chain. gcc is a compilation tool chain.

1.2 significance of pretreatment

  • The main purpose of the compiler itself is to compile the source code and convert the source code written in c language into assembly code;
  • The preprocessor helps the compiler do some pre compilation chores;

1.3 common pretreatment

In gcc: gcc   - E   xx.c   - o   xx.i   // Preprocessing only, no compilation

(1) #include < > and #include ''   ''

  • < > is specifically used to contain header files provided by the system, and "" is used to contain header files written by yourself;
  • < > the compiler will only go to the directory specified by the system to find the header file (the hidden meaning is that it will not find it in the current directory). The compiler also allows the command - I to attach other specified paths (this path is generally a directory written by itself for storing header files);
  • "The compiler will search for the corresponding header file in the current directory by default. If it is not found, it will search in the specified directory of the system

(2) Conditional compilation

(2 messages) conditional compilation_ Shrimp_ Millet CSDN blog_ Conditional compilationhttps://blog.csdn.net/Shrimp_millet/article/details/94574406

2. Macro definition

Remember: a macro definition is an intact replacement. Macro definition replacement will be performed recursively until the replaced value is no longer a macro. Each parameter of a macro body with parameters should be enclosed in parentheses.

Example 1 - MAX macro

  1 #include <stdio.h>
  2 #define MAX(a,b) ((a)>(b))?(a):(b)
  4 int main(void)
  5 {
  6   int x=2,y=3;
  8   int max = MAX(x+3,y);
 10   printf("max = %d\n",max);
 12   return 0;
 13 }

Example 2 - how many seconds are there in a year with a macro definition


#define SEC_PER_YEAR (365*24*60*60) / / first, if a number is declared in the computer, it defaults to int, and (365 * 24 * 60 * 60) just exceeds the maximum value that int can represent

How to solve it? The macro body is expressed as an unsigned number, which is written as:

#define SEC_PER_YEAR (365*24*60*60UL)

2.1 difference between macro with parameters and function with parameters

  • Macro definitions are processed during real preprocessing, while functions are processed during compilation;
  • The choice of the two depends on the calling cost of the function. For example, if the function body has little (or very simple) content, it is suitable to use macro definition;
  • A macro with parameters does not check the parameter type, but the parameters and return values of the function have explicit types;

2.2 inline functions and inline keywords

    Function: it not only has the advantage of the function itself with check parameters, but also has the advantage of macro with parameters (without calling overhead, but expand in place). When the function body is very short, you can use inline functions.

    The inline function is directly copied and "embedded" into the main function, that is, the code of the inline function is directly placed in the position of the inline function, which is different from the general function. When the main function calls the general function, the instruction jumps to the entry address of the called function. After executing the called function, the instruction jumps back to the main function to continue executing the following code; Because the inline function directly places the code of the function in the position of the function, there is no instruction jump, and the instructions are executed in sequence  .

    Generally, there is only one code segment of a function, which is placed in a certain position in memory. When the program calls it, the instruction jumps over; The next time the program calls it, the instruction jumps over again; The inline function is the code that calls several inline functions in the program, and the inline function will copy several copies in the corresponding position. So the disadvantage is that it takes up a lot of space.

3. Function

be careful:

  • Don't pass too many parameters. If you really need it, package it with a structure;
  • Try not to touch global variables;

3.1 recursive function

Example 1 - factoring a number

#include <stdio.h> 

int recursive(int n)
    int result;
    if (n<0)                                          //Judge exceptions
        printf("Input error!\n");
        return 0;
    else if (n==0 || n==1)
        result = 1;  //Push back wall
        result = recursive(n-1) * n;  //Recurrence relationship, the relationship between this number and the previous number.
    return result;

int main()
    int n = 5;                                              //Enter the number 5 to calculate the factorial of 5
    printf("%d factorial =%d",n,recursive(n));
    return 0;

Example 2 - finding Fibonacci sequence

F(n)=   F(n-1)+F(n-2),F(0)= 0,F(1)= F(2)= 1 //   This sequence starts with the third term, and each term is equal to the sum of the first two terms

#include <stdio.h>

long fibonacci( long num )
    if ( num == 0 || num == 1 )
        return num;
        return fibonacci( num -1 ) + fibonacci( num -2 );

void main()
    long number;
    puts("Please enter a positive integer: ");
    scanf("%ld", &number);
    printf("Fibonacci sequence No%ld Item is: %ld\n", number, fibonacci( number ) );


3.2 basic principle of recursion

#include <stdio.h>

void recursive(int n);

int main(void)
	int n = 1;
	return 0;
void recursive(int n)
	printf("The first%d Layer, address:%p\n",n,&n);   //Before recursion
	printf("The first%d Layer, address:%p\n",n,&n);   //After recursion 

    How to understand? You can imagine a function call chain. Fun1 calls fun2, fun2 calls fun3, and fun3 calls fun4; When fun4 meets   After the end condition, the control returns fun3 - > fun2 - > fun1 in turn. But fun1~fun4 in recursion are the same functions.


  • Each call returns once. After the function is executed, control will be passed back to the upper level of recursion. The program must return recursion level by level in order;
  • The statements before recursion in recursive functions are executed in sequence;
  • The statements after recursion in recursive functions are executed in reverse order;
  • There must be a condition to terminate recursion;

    Recursive functions are actually executed on stack memory, and each recursion will occupy some stack memory. If not terminated, it may cause stack overflow.

4. String function

  • memcpy: two spaces cannot overlap
  • memmove: allow two spaces to overlap
  • memset
  • memcmp
  • strcpy
  • strcat
  • strcmp

5. Make your own static link library and dynamic link library

Tags: C

Posted on Sat, 30 Oct 2021 07:40:02 -0400 by Kaitosoto