当前位置:网站首页>C language: 14. Preprocessing
C language: 14. Preprocessing
2022-07-27 19:33:00 【Brother rabbit head】
c Language :14、 Preprocessing
1、c Program execution
c Language programming executable files are divided into 4 Step :
Preprocessing : from .c The file to .i The process of file processing is called preprocessing
compile :.i File generation .s The process of file compilation
assembly :.s File generation .o The process of documentation is called assembly
link :.o The process of generating executable files from files is called linking
2、 Preprocessing
The parameters below -E Express gcc Just do pretreatment .
The following command will helloworld.c Pretreatment into a helloworld.i Preprocessing files .gcc -o helloworld.i helloworld.c -E
The following figure shows the header file stdio.h And hellworld.c The code in the file is carried out and merged .
The first thing preprocessing does is to expand the header file , At the same time, macro replacement should also be carried out during preprocessing .
2、 Macro replace
#include <stdio.h>
#define R 10
int main()
{
int a = R;
printf("a=%d\n", a);
printf("hello world");
return 0;
}
Preprocess the above code , Check the preprocessing file again , Here's the picture 
It can be seen from the above figure , Pretreatment stage , The macro will be replaced with the corresponding value by the compiler .
Be careful : Macro replacement is just a simple string replacement , Will not consider c Grammatical , As code below .
#include <stdio.h>
#define M int main(
M){
printf(" At the top of the M Will be replaced by int main(\n");
return 0;
}
The above code has been preprocessed gcc -o helloworld.i helloworld.c -E after ,helloworld.i The content in is as follows 
2.1、 Macro functions
#include <stdio.h>
#define R 20
#define M int main(
#define N(n) n*10
M){
int a = R;
printf("a=%d\n", a);
int b = N(a);//N(a) replaced a*10
printf("b=%d\n",b);
return 0;
}
The result of code preprocessing above , Here's the picture :
2.2、 Macro function considerations ( Operator priority )
Because macro functions directly replace characters , So you need to pay attention to the errors in the figure below

The macro function definition above must be bracketed
#include <stdio.h>
#define ADD(a,b) (a+b)
int main(){
int b = ADD(2,3) * ADD(2,3);
printf("b=%d\n",b);
return 0;
}

2.3、 Advantages of macro functions

2.4 Conditional compilation
@todo
边栏推荐
猜你喜欢
随机推荐
带来高价值用户体验的低代码开发平台
Basic use of Nacos (1) - getting started
浅谈基本的网络基本故障和问题排查
5W奖金池/面向高校,2022法律科技创新大赛报名火热进行中
Automatic testing of Web UI: Selenium syntax explanation is the most complete in the history
Using vscode to build u-boot development environment
HDU1171_Big Event in HDU【01背包】
200行代码快速入门文档型数据库MonogoDB
Double insurance for line breaking
SSM project uses filter to realize login monitoring
Can set be used to define local variables in OPDS SQL
阿里云对象存储OSS的开通和使用
Some advice for NS2 beginner.
SQL Server top keyword usage
Error analysis of building Alibaba cloud +typera+picgo map bed
Subscription and use of Alibaba cloud video on demand service
C language case: password setting and login > clear solution getchar and scanf
Word 2007+ tips
Code interview of Amazon
sql 字段类型转换









