当前位置:网站首页>C language preprocessing command [i]
C language preprocessing command [i]
2022-07-18 11:32:00 【jie3606】
Preprocessing
What are preprocessing commands ?
C Language source files should go through compile 、 link To generate an executable program :
- compile (Compile) The source file (.c file ) Convert to target file . about VC/VS, The suffix of the target file is .obj; about GCC, The suffix of the target file is .o.
Compilation is for a single source file , Only one source file can be compiled at a time , If there are multiple source files in the program , You need to compile multiple times .
- link (Link) Is for multiple files , It will compile multiple target files and libraries in the system 、 Components, etc. are combined into an executable program .
In actual development , Sometimes you need to simply process the source file before compiling .
for example : The header file definition and function name of the system are different in different environments , for instance : If you want to develop one now C Language program , Let it pause 5 Output the content in seconds , And requires cross platform , stay Windows and Linux It can run in the next place , What shall I do? ?
The difficulty of this program is , The pause function and header file are different in different platforms :
- Windows The prototype of the pause function under the platform is void Sleep(DWORD dwMilliseconds)( Be careful S It's in capital letters ), The unit of the parameter is “ millisecond ”, be located <windows.h> The header file .
- Linux The prototype of the pause function under the platform is unsigned int sleep (unsigned int seconds), The unit of the parameter is “ second ”, be located <unistd.h> The header file .
Different functions must be called under different platforms , And introduce different header files , Otherwise, it will lead to compilation errors , because Windows Not under the platform sleep() function , either <unistd.h> The header file , vice versa . This requires us to compile before , That is, the preprocessing stage to solve this problem . Look at the code below :
#include <stdio.h>
// Introduce different header files under different platforms
#if _WIN32 // distinguish windows platform
#include <windows.h>
#elif __linux__ // distinguish linux platform
#include <unistd.h>
#endif
int main() {
// Different platforms call different functions
#if _WIN32 // distinguish windows platform
Sleep(5000);
#elif __linux__ // distinguish linux platform
sleep(5);
#endif
puts("hello jei");
return 0;
}
When this function is in linux After preprocessing, it is equivalent to :
#include <stdio.h>
#include <unistd.h>
int main() {
sleep(5);
puts("hello jie");
return 0;
}
These processes of simple processing of source files before compilation , It's called preprocessing ( That is, pretreatment 、 Deal with in advance ).
- Pretreatment is mainly to # The first order , for example #include <stdio.h> etc. . Preprocessing commands should be placed in all Outside of the function , And it's usually placed in front of the source file .
- Pretreatment is C An important function of language , By the preprocessor . When compiling a source file , The system will automatically call the preprocessor to process the preprocessing part of the source program , After processing, it will automatically enter the compilation of the source program .
- The compiler saves the preprocessed results to a file with the same name as the source file .i In file , for example main.c The preprocessing results of are in main.i in . and .c equally ,.i It's also a text file , You can open it with an editor and view the content directly .
- C Language provides a variety of preprocessing functions , Such as Macro definition 、 File contains 、 Conditional compilation etc. , Using them properly will make the program written Easy to read 、 modify 、 Porting and debugging , It is also conducive to modular programming .
The source file contains
Contains another source file , To the position immediately on the next line of the instruction in the current source file .
- #include < file name > (1)
- #include “ file name ” (2)
take file name The identified source file contains (copy) To the current source file
Search for files in the system directory
Search for files in user-defined directories , If not found, then (1) Look under
Be careful :
- Define functions and global variables in the header file 」 This perception is a mistake of principle ! Whether it's a standard header file , Or a custom header file , Can only contain declarations of variables and functions , Cannot contain definition , Otherwise, repeated definition errors will be caused when importing multiple times .
- in other words , Using double quotation marks is one more search path than using angle brackets , It's more powerful .
Macro definition #define
#define Simple string substitution
#include "stdio.h"
//define N 100
#define STR "liujie"
int main(){
int a[N] = {
};
printf("%lud ",sizeof (a));
puts(STR);
return 0;
}
//[email protected]:~/tmp/LinuxC# gcc -DN=10 main.c -o main && ./main
//40d liujie
Macros with arguments :
- A great advantage of replacing functions with macros like functions is ∶ Macro replacement improves the execution speed of code , Because there is no function call on
pin . However , If the macro of a similar function is large , Then this speed increase will be caused by repeated code Increase in program length .
( Code bloat )
#include "stdio.h"
#define ABS(x) (x)>0?(x):-(x)
int main(){
printf("%d ", ABS(2-3));
return 0;
}
#、 ##、 @#、 #undef
# Operator , Often referred to as stringifie( String ) Operator , So that the argument after it is converted into a string with double quotation marks
#include <stdio.h>
#define mkstr(s) #s
int main(void) {
printf(mkstr(I Like C));
return 0;
}
// >>I like C
## The operator , be called pasting( Paste , link ) Operator , Used to connect two symbols
#include <stdio.h>
#define concat(a,b) a##b
int main(void) {
int xy = 10;
printf("%d",concat(x,y)); // The dummy real combination of macro does not have a type , There is no type conversion .
// printf("%x",concat("hello","tulun"));
return 0;
}
//>>10
#@ The function of is to character the arguments behind it VS Effective in the environment
#include<stdio.h>
#define TO_CHAR(x) #@x
int main() {
printf("%c", TO_CHAR(1));
printf("%c", TO_CHAR(3));
printf("%c", TO_CHAR(4));
printf("%c", TO_CHAR(5));
return 0;
}
#undef Instruction undefined identifier , That is, it cancels the previous #define Yes identifier The definition of . If the identifier has no macro associated with it , Then ignore this instruction .
macro body Macro expansion process
- 1. to # stringified operation , String only arguments .
- 2. Right again Argument Conduct Replace .
- 3. perform ## pasted operation .
- 4. For the expanded results Check to see if there is #define Defined symbols , If the above process is repeated , but The second expansion of the same macro name is not allowed .
example :
Variable parameter macros
Predefined macros
Conditional compilation
reference
home page > C Introduction to language > Preprocessing command
C++ chinese - API Reference documents
Tencent classroom -> All courses IT· Internet background development C/C++C Language introduction to advanced
边栏推荐
猜你喜欢

Memory management: memory allocation and recycling

subset sum problem

Flink(六)容错机制

Reading the pointpillar code of openpcdet -- Part 1: data enhancement and data processing

Flink (VII) Flink SQL

EMQX 服务器建立SSL/TLS安全连接、单向、双向

Talking about software defect management

编辑距离问题

Low code rendering

Static library and dynamic library
随机推荐
What is the once brilliant streaming media protocol RTMP? Can it be completely replaced? Yiwen takes you into the world of RTMP
How does the trend of banking situation in London come into being
The secret of the three moving averages in the spot gold trend chart
伦敦银行情走势怎样产生
Filter
[SQL interview question] find the number of users who have clicked three times in a row, and there can be no other people's clicks in the middle
flutter showDialog弹窗
Valgrind
jeesite登录流程
转本结束暑假2022.6.29-7.13我的深圳之行(体验)
Flink (I) overview
Flink (II) time and window
Installation method of memory module on dual channel (Dual CPU) server motherboard
Low code rendering
02. Resttemplate learning notes
subset sum problem
[development tutorial 2] crazy shell arm function mobile phone - Introduction to test program
编辑距离问题
劍指 Offer 64. 求 1 + 2 + … + n
微信小程序如何实现下拉刷新?