当前位置:网站首页>Part II - C language improvement_ 11. Pretreatment
Part II - C language improvement_ 11. Pretreatment
2022-07-26 23:26:00 【qq_ forty-three million two hundred and five thousand two hundr】
C The four steps of language processing for source program : Preprocessing 、 compile 、 assembly 、 link .
Preprocessing is before the program source code is compiled , By preprocessor (Preprocessor) Processing of program source code . This process does not parse the source code syntax of the program , But it will split or process the source code into specific symbols to prepare for the next compilation .
11.1 The file contains instructions (#include)
11.1.1 The file contains processing
“ The file contains processing ” It means that one source file can include all the contents of another file .C Language provides #include The command is used to implement “ File contains ” The operation of .

11.1.2 #incude<> and #include"" difference
- "" The presentation system First stay file1.c Current directory found file1.h, If you can't find it , Again Search according to the directory specified by the system .
- < > The presentation system direct Search according to the directory specified by the system .
Be careful :
1. #include <> It is often used in header files containing library functions ;
2. #include "" It is often used to include customized header files ;
3. Theoretically #include Can contain files of any format (.c .h etc. ) , But it's usually used for The header file The inclusion of ;
11.2 Macro definition
11.2.1 Macro definition without parameters ( Macro constants )
If it is widely used in the program 100 This value , So for the convenience of management , We can define it as :
const int num = 100; But if we use num Define an array , No support for c99 Standard compilers do not support , because num Not a compiler constant , If you want to get a compiler constant , Then you can use :
#define num 100
During compilation preprocessing , Put all the num Use both 100 Instead of . This method allows the user to replace a long string with a simple name , The process of replacing the macro name with a string at precompile is called “ Macro expansion ”. Macro definition , It only works in the file of macro definition .
#define PI 3.1415
void test()
{
double r = 10.0;
double s = PI * r * r;
printf("s = %lf\n", s);
}Output results
s = 314.150000explain :
- Macro names are usually capitalized , To distinguish it from variables ;
- Macro definitions can be constants 、 Expression etc. ;
- Macro definitions are not checked for syntax , Only after the compilation is expanded, the source program will report an error ;
- The valid range of macro name is from definition to the end of this source file ;
- It can be used #undef Command to terminate the scope of the macro definition ;
- In the macro definition , You can refer to the defined macro name ;
11.2.2 Macro definition with parameters ( Macro functions )
In the project , Often write some short and frequently used functions as macro functions , This is because macro functions do not have ordinary function parameters on the stack 、 Jump 、 The cost of returning and so on , It can improve the efficiency of the program .
Macros use parameters , You can create function like macros with shapes and functions similar to functions . Macro parameters are also enclosed in parentheses .
#define SUM(x,y) (( x )+( y ))
void test()
{
// Just do text substitution The following example is replaced by int ret = ((10)+(20));
int ret = SUM(10, 20);
printf("ret:%d\n",ret);
}Output results
ret:30Be careful :
- The name of macro No spaces in , But there can be spaces in the replaced string .ANSI C Allow spaces in parameter lists ;
- Enclose each parameter in parentheses , And the overall definition of the macro .
- Use capital letters to represent the function name of the macro .
- If you plan to use macros instead of functions to speed up the program . If you only use macros once in the program, the running time of the program will not be greatly improved .
11.3 Conditional compilation
11.3.1 Basic concepts
In general , All the lines in the source program are compiled . But sometimes you want to compile some source program lines only when certain conditions are met , That is, specify the compilation conditions for this part of the source program line .

11.3.2 Conditional compilation
Prevent header files from being repeatedly referenced ;
#ifndef _SOMEFILE_H
#define _SOMEFILE_H
// Variables that need to be declared 、 function
// Macro definition
// Structure
#endif11.4 Some special booking macros
C compiler , Several special forms of predefined macros are provided , It can be directly used in practical programming , Very convenient .
// __FILE__ The source file name of the macro file
// __LINE__ The line number of the macro's line
// __DATE__ The date the code was compiled
// __TIME__ The time the code was compiled
void test()
{
printf("%s\n", __FILE__);
printf("%d\n", __LINE__);
printf("%s\n", __DATE__);
printf("%s\n", __TIME__);
}Output results
E:\TortoiseGit-2.8.0.0-64bit\cs_-c\C_2022_07_26\C_2022_07_26\test.c
40
Jul 26 2022
11:03:04边栏推荐
- 2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ )
- HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理
- Silicon Valley class lesson 5 - Tencent cloud object storage and course classification management
- About statefulwidget, you have to know the principle and main points!
- 你知道磁环电感的常见磁芯类型有哪些吗?
- How can enterprises mitigate the security risks of Internet of things and industrial Internet of things
- MySQL random paging to get non duplicate data
- P5469 [NOI2019] 机器人(拉格朗日插值、区间dp)
- 第二部分—C语言提高篇_7. 结构体
- ESMFold: AlphaFold2之后蛋白质结构预测的新突破
猜你喜欢

Product principles of non-financial decentralized application

Easily implement seckill system with redis! (including code)

Programmer growth chapter 29: how to motivate employees?

MySQL 数据的导入

HCIA-R&S自用笔记(20)VLAN综合实验、GVRP
![[shader realizes swaying effect _shader effect Chapter 4]](/img/ab/bdbc4a0f297541b532af81a49e2633.png)
[shader realizes swaying effect _shader effect Chapter 4]

HCIA-R&S自用笔记(18)园区网架构基础、交换机工作原理、VLAN原理

Science | 华盛顿大学利用AI和结构预测设计全新蛋白质

为什么我还在CSDN写文章?一段陪伴学习的历程。

Professor Ashe, a Chinese scientist, made a positive response to the suspected fake Nature paper
随机推荐
2. Realize the map of navigation bar and battle page
蔚来杯2022牛客暑期多校训练营2
TypeScript阶段学习
[untitled]
SQL 基础知识
基本的SELECT语句
After working for one year, I have some insights (written in 2017)
HCIA-R&S自用笔记(20)VLAN综合实验、GVRP
Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum
Kingbasees SQL language reference manual of Jincang database (3.1.1.14. scope type)
How to recover the original data when the U disk is damaged, and how to recover the damaged data when the U disk is damaged
[Luogu] p2341 popular cattle
18、打开、保存文件对话框使用小记
Will the approval in advance affect the formal approval?
Cloud native microservices Chapter 1 server environment description
第二部分—C语言提高篇_5. 位运算
2019 biometric forum successfully ended: these ten highlights should not be missed!
ESMFold: AlphaFold2之后蛋白质结构预测的新突破
Six challenges facing enterprise data governance!
【MySQL】CentOS 7.9安装、使用MySQL-5.7.39二进制版