当前位置:网站首页>[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

边栏推荐
- WBC learning notes (II): practical application of WBC control
- 深度学习论文阅读目标检测篇(七)中文版:YOLOv4《Optimal Speed and Accuracy of Object Detection》
- 1.Intro_ Math (white board derivation and reprint of station B)
- Use of PCL
- [resolved]setonnavigationitemselectedlistener() deprecated
- Efficiency thesis Reading 1
- Install Anaconda + NVIDIA graphics card driver + pytorch under win10_ gpu
- Detailed explanation of traditional image segmentation methods
- Using MySQL and Qt5 to develop takeout management system (I): environment configuration
- Sqoop merge usage
猜你喜欢

Isinstance() function usage

Section IV HQL execution process

Principle of playing card image segmentation

Whale conference provides digital upgrade scheme for the event site

Mongodb分片环境搭建和验证(redis期末大作业)

Using MySQL and Qt5 to develop takeout management system (I): environment configuration

Convex optimization of quadruped

Remote centralized control of distributed sensor signals using wireless technology

Compiling owncloud client on win10
![[unity mirror] use of networkteam](/img/b8/93f55d11ea4ce2c86df01a9b03b7e7.png)
[unity mirror] use of networkteam
随机推荐
Convert verification code image to tfrecord file
Slider verification - personal test (JD)
Efficiency thesis Reading 1
Polka lines code recurrence
Computer mall based on SSM
隐藏式列表菜单以及窗口转换在Selenium 中的应用
1.23 neural network
直播回顾 | smardaten李鸿飞解读中国低/无代码行业研究报告:风向变了
深度学习论文阅读目标检测篇(七)中文版:YOLOv4《Optimal Speed and Accuracy of Object Detection》
WBC learning notes (I): manually push WBC formula
1.20 study univariate linear regression
Detailed explanation of traditional image segmentation methods
Yolov5进阶之二安装labelImg
9. code generation
Data warehouse (1) what is data warehouse and what are the characteristics of data warehouse
滑块验证 - 亲测 (京东)
Implementation of ffmpeg audio and video player
Sublime Text3 common plug-ins
Drawing with MATLAB (2) -- color ring
ImportError: ERROR: recursion is detected during loading of “cv2“ binary extensions. Check OpenCV in

