C language-------macro #define

Everyone knows that #define is a preprocessing instruction, so how can we see the preprocessing results? Let's do a little test.
Create two new files test.h and demo.c, the contents of which are as follows:
test.h

#ifndef TEST_H
#define TEST_H
1
#define CONFIG
2
int a;
3
#endif //  TEST_H

demo.c

#include "test.h"
4
int main(void)
{
    
    
    CONFIG hello
    CONFIG hello
    return 0;
}

Use gcc -Ethe command to view the preprocessing results such as macro expansion after the code is preprocessed as follows:

$gcc -E demo.c
# 1 ".\\demo.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 ".\\demo.c"
# 1 ".\\test.h" 1


1

2
int a;
3
# 2 ".\\demo.c" 2
4
int main(void)
{
    
    
    hello
    hello
    return 0;
}

It can be seen that the original #lines of code at the beginning have disappeared, and the preprocessing imports the content in test.h and CONFIGreplaces it with .
Interested students can read the section 1.3 Standard I/0 Library and C Preprocessor in the book "C Expert Programming" to learn about the history of preprocessors.

Guess you like

Origin blog.csdn.net/qq_43577613/article/details/127811786