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

边栏推荐
- Digital image processing learning (II): Gaussian low pass filter
- Matlab function foundation (directly abandon version)
- Realizing sequence annotation with transformers
- How to correctly PIP install pyscipopt
- Exploration of webots and ROS joint simulation (I): software installation
- 9. code generation
- Intra class data member initialization of static const and static constexpr
- The solution of positioning failure caused by framework jump
- Implementation of ffmpeg audio and video player
- 什么是乐观锁,什么是悲观锁
猜你喜欢

Euler function: find the number of numbers less than or equal to N and coprime with n

Remote centralized control of distributed sensor signals using wireless technology

基于SSM的毕业论文管理系统

Koa_ mySQL_ Integration of TS

框架跳转导致定位失败的解决方法

Digital image processing learning (II): Gaussian low pass filter

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

【云原生 | Kubernetes篇】深入万物基础-容器(五)

Degree of freedom analysis_ nanyangjx

Selenium builds cookies pool to bypass authentication and anti crawl login
随机推荐
框架跳转导致定位失败的解决方法
pgsql_ UDF01_ jx
【微积分】拉格朗日乘子法
Relation extraction model -- spit model
Analysis of Yolo series principle
KNN resolution
Pytorch neural network
在哪个软件上开户比较安全
The solution of positioning failure caused by framework jump
攔截器與過濾器的實現代碼
WBC learning notes (I): manually push WBC formula
Sqoop merge usage
三菱PLC若想实现以太网无线通讯,需要具备哪些条件?
Clion installation + MinGW configuration + opencv installation
力扣399【除法求值】【并查集】
Efficiency thesis Reading 1
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(一)
isinstance()函数用法
[QNX Hypervisor 2.2用户手册]12.1 术语(一)
Formula understanding in quadruped control

