当前位置:网站首页>[program compilation and pretreatment]
[program compilation and pretreatment]
2022-06-26 08:57:00 【Always brush questions every day】

1. The translation environment of the program &2. execution environment
C Two environments for language program implementation :
First step : Translation environment -- Make the source program into machine executable machine instructions
The second step : execution environment -- Implement executable code

3. Detailed explanation : Compilation and linking of programs ( Translation environment )

Multiple test.c file , Multiple test.obj, Generate a test.exe
Compiler introduction :

Link library : Library functions in library files / Third party Library

4. Detailed explanation of pretreatment symbols
4-1 Built in preprocessing symbols
int main()
{
for (int i = 0; i < 10; i++)
{
printf("name:%s\tfile:%s \tline:%d \tdate:%s \ttime:%s \ti:%d\n",__func__,__FILE__, __LINE__, __DATE__, __TIME__);
}
return 0;
}
5. Preprocessing instruction
5-1#define Defining symbols
#define NUM 100
#define STR "hello world"// Strings can also use preprocessing to define symbols 
5-2#define Defining macro
#define MAX(x,y) ((x)>(y)?(x):(y))
int main()
{
int a = 10;
int b = 20;
int c = MAX(a, b);
printf("%d\n", c);
return 0;
}Be careful :
- #define Define symbols and macros without semicolons
- The left parenthesis of the parameter list must be the same as name Next door neighbor ( A function can , Macros cannot )
- When writing macros , Don't be stingy with the parentheses for parameters
#define NUM 100;// Wrong use cases 1
#define DOUBLE (x) x*x// Wrong use cases 2 and 35-3#define Replacement rules
#define M 100
#define DOUBLE(x) ((x)+(x))
int main()
{
int a = DOUBLE(M);
printf("%d\n", a);
return 0;
}
// First step :- Replace M- int a=DOUBLE(100)
// The second step :- Replace X- #define DOUBLE(100) 200
// The third step :- Replace DOUBLE(100)- int a=200;
6.# and ## The magic of macro
6-1#
6-1-1 Example 1: Simply study the information of auxiliary printing , The type of parameter is not considered
ask : How to insert parameters into a string ?
idea 2: function
//void Print(int n)
//{
// printf("the value of n is &d\n", n);
//}
// idea 3: macro
//#define PRINT(N) printf("the value of N is %d\n",N)// idea 3
//int main()
//{
// int a = 10;
// //printf("the value of a if %d\n", a);
// Print(a);
//
// int b = 20;
// //: idea 1: One by one
// //printf("the value of b is %d\n", b);
// PRINT(b);
//
//
// return 0;
//}
// idea 4:( The best way to satisfy users )#
#define PRINT(N) printf("the value of "#N" is %d\n",N)
int main()
{
// footstone
printf("hello world\n");
printf("hello ""world\n");
int a = 10;
PRINT(a);
// Equivalent to :printf("the value of ""a"" is %d\n",N);
return 0;
}

6-1-2: Consider the type of parameter passed in ( This makes me think of function overloading )
#define PRINT(N) printf("the value of "#N" is %d\n",N)
int main()
{
int a = 10;
double pai = 3.14;
PRINT(a);
PRINT(pai);
return 0;
}6-2##
effect :## You can combine the symbols on both sides of it into one symbol
It allows macro definitions to create identifiers from detached pieces of text
#define CAT(name,num) name##num
int main()
{
int song100 = 105;
printf("%d\n", CAT(song, 100));
// Equivalent to printf("%d\n",song100)
return 0;
}
Here I want to explain one thing :
explain : Advanced line pretreatment ( First synthesized classi), To compile
6-3 Macro parameters with side effects
++ Side effects in macros
#define MAX(m,n) ((m)>(n)?(m):(n))
int main()
{
//int a = 0;
//int b = a + 1;
//b = a++;// Statements with side effects
// Macro parameters with side effects
int a = 10;
int b = 20;
int c = MAX(a++, b++);
// amount to int c=(a++)>(b++)?(a++):(b++);
// 11 21 22
printf("%d\n", a);//11
printf("%d\n", b);//22
printf("%d\n", c);//21
return 0;
}reason :
- Macro parameters are replaced without calculation ( The arguments to the function are with a computed copy )
- If there are multiple copies in the macro ++ It will be executed many times
7. The comparison between macro and function ( Blue indicates the angle of consideration )
The comparison between macro and function
The advantages of macro :
- There are no function calls and function returns expenses
- Macro parameters are the same as type irrelevant
The disadvantages of macro :
- Macro is no way debugging Of
- Macros are not used properly , Operators may be introduced priority and ++ Of side effect problem
- Macro has no way recursive Of
8. Conditional compilation
application :stdio.h There are many such things in the header file , You must understand
#define NUM 1
int main()
{
//#if-#else-#endif Conditional compilation of branches
#if 0
printf("hehe\n");
#else
printf("haha\n");
#endif
//#if-#elif-(#else)-#endif Multi branch conditional compilation
#if NUM==1
printf("1\n");
#elif NUM==2
printf("2\n");
#else
printf("0\n");
#endif
// Determine whether #define Two methods of symbols
// Method 1:
#if defined(NUM)
printf("1\n");
#endif
// Method 2:
#ifdef NUM
printf("2\n");
#endif
// Determine whether #undefine Two methods of symbols
// Method 1:
#if !defined(NUM)
printf("1\n");
#endif
// Method 2:
#ifndef NUM
printf("2\n");
#endif
return 0;9. Preprocessing instruction #include
9-1#include<stdio.h> and #inlcude"stdio.h" The difference between
Search strategy :
#include“include”: First look in the files in the root directory , If you don't find it, go to the target database to find it
#include<stdio.h>: Go directly to the target database to find
So your #include<stdio.h> It can be written. #include"stdio.h"
But your contact.c You can't put #include"conta
ct.h" It's written in #include<contact.h>

9-2 Two ways to prevent header files from being included repeatedly :( Written in the header file )
The harm of including header files many times : Added thousands of lines of code , This makes the compiler under great pressure
Method 1:
//test.c
#include<stdio.h>
#include"stdio.h"
#include<stdio.h>
//test.h
#ifndef __TEST_H__
#define __TEST_H__
#endifMethod 2:
//test.c
#include<stsdio.h>
#include<stdio.h>// Invalid , This time the header file is not included
#test.h
#pragma once

边栏推荐
- [unity mirror] use of networkteam
- Leetcode notes: binary search simple advanced
- Addition of attention function in yolov5
- 多台三菱PLC如何实现无线以太网高速通讯?
- The principle and function of focus
- 三菱PLC若想实现以太网无线通讯,需要具备哪些条件?
- Digital image processing learning (II): Gaussian low pass filter
- Detailed explanation of traditional image segmentation methods
- Convex optimization of quadruped
- 在同花顺开户证券安全吗,
猜你喜欢

WBC learning notes (II): practical application of WBC control

Fourier transform of image

Corn image segmentation count_ nanyangjx

Detailed explanation of traditional image segmentation methods

Yolov5进阶之四训练自己的数据集

Regular Expression 正则表达式

基于SSM的电脑商城

Fast construction of neural network

Trimming_ nanyangjx

Principle of playing card image segmentation
随机推荐
唯品会工作实践 : Json的deserialization应用
Performance comparison of unaryexpr's function on matrix elements in eigen Library
XSS cross site scripting attack
Simulation of parallel structure using webots
[QNX Hypervisor 2.2用户手册]12.1 术语(一)
框架跳转导致定位失败的解决方法
1.21 study logistic regression and regularization
Record the problem yaml file contains Chinese message 'GBK' error
Optimize quiver function in MATLAB to draw arrow diagram or vector diagram (1) -matlab development
isinstance()函数用法
Exploration of webots and ROS joint simulation (I): software installation
滑块验证 - 亲测 (京东)
KNN resolution
Uniapp uses uparse to parse the content of the background rich text editor and modify the uparse style
Trimming_ nanyangjx
关于小程序tabbar不支持传参的处理办法
Backward usage
Remote centralized control of distributed sensor signals using wireless technology
ImportError: ERROR: recursion is detected during loading of “cv2“ binary extensions. Check OpenCV in
自动化测试中,三种常用的等待方式,强制式(sleep) 、 隐式 ( implicitly_wait ) 、显式(expected_conditions)

