当前位置:网站首页>C language program environment and preprocessing
C language program environment and preprocessing
2022-07-20 08:31:00 【Feverish CPU】
Catalog
2、 The stage of compiling itself
(5) Macro parameters with side effects
(6) Macro and function comparison
(1) How header files are included
There are two kinds of program environments :
Translation environment : The source code is converted into executable machine instructions
execution environment : Used to actually execute code
One 、 compile + link
1、 Translation environment
The source file is converted into the target file through the compilation process , Each object file is bound together by a linker , Standards will also be introduced C Any function in the function library used by the program , And it can search the programmer's personal library , Link the functions it needs to the program .
2、 The stage of compiling itself
Drawing description :
The code illustrates :
//sum.c
int g_val = 2016;
void print(const char* str)
{
printf("%s\n", str);
}
//test.c
include<stdio.h>
int main()
{
extern void print(char* str);
extern int g_val;
printf("%d\n", g_val);
print("hello bit.\n");
return 0;
}
//test.c
#include<stdio.h>
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", i);
}
return 0;
}
By means of gcc View what happens at each step during compilation
a. Preprocessing options gcc -E test.c -o test.i
Stop after the pretreatment is complete , All the results after pretreatment are put in test.i In file
b. Compilation options gcc -S test.c
Stop when the compilation is complete , The results are stored in test.s in
c. assembly gcc -c test.c
Stop when the assembly is finished , The results are stored in test.o in
3、 Running environment
The process of program execution :
1. The program must be loaded into memory . In an operating system environment : This is usually done by the operating system . In an independent environment , The loading of the program must be arranged manually , It can also be done by putting executable code into read-only memory .
2. The execution of the procedure begins . And then I call main function .
3. Start executing program code . At this point, the program will use a runtime stack (stack), Store function local variables and return address . Programs can also use static (static) Memory , Variables stored in static memory retain their values throughout the execution of the program .
4. To terminate the program . Normal termination main function ; It could be an accidental termination .
Two 、 Pretreatment details
1、 Predefined symbols
__FILE__ // Source files to compile
__LINE__ // The current line number of the file
__DATE__ // The date the file was compiled
__TIME__ // When the file was compiled
__STDC__ // If the compiler follows ANSI C, Its value is 1, Otherwise, it is not defined
example :
int main()
{
printf("file:%s\nline:%d\n", __FILE__, __LINE__);
return 0;
}
2、#define
(1)#define Define identifier
for example :
#define print printf("hehe\n")
#define STR "hello bit"
#define MAX 100
#define reg register// by register This keyword , Create a short name
#define do_forever for(;;)// Replace an implementation with a more vivid symbol
#define CASE break;case// Writing case Automatically put break write
// If you define stuff Too long , It can be divided into several lines , Except for the last line , Add a backslash after each line ( Line continuation operator )
#define DEBUG_PRINT printf("file:%s\tline:%d\t \
date:%s\ttime:%s\n" ,\
__FILE__,__LINE__ , \
__DATE__,__TIME__ )
Be careful : stay define When defining an identifier , Don't add... At the end ;
(2)#define Defining macro
Macro statement :
#define name( parament-list ) stuff
reminder : Macro definitions that evaluate numeric expressions should be bracketed , Avoid unexpected interactions between operators in parameters or adjacent operators when using macros .
(3)#define Replacement rules
a. When calling a macro , First, check the parameters , Replacement by #define Defined symbols
b. The replacement text is then inserted into the program at the location of the original text
c. Repeat the above steps
for example :
Be careful :a. Macros cannot have recursion
b. When the preprocessor searches #define When defining symbols , The contents of string constants are not searched
(4)# and ##
// The output of such code is get meney
int main()
{
char* p = "get ""money\n";
printf("get ""money\n");
printf("%s", p);
return 0;
}
When the string is used as a macro parameter, you can put the string in the string :
#define PRINT(FORMAT, VALUE) printf("the value is "FORMAT"\n", VALUE);
int main()
{
PRINT("%d", 10);
return 0;
}
Use #, Change a macro parameter into the corresponding string :
#define PRINT(FORMAT,VALUE) printf("the value of "#VALUE" is "FORMAT"\n",VALUE);
int main()
{
int i = 10;
PRINT("%d", i + 3);
return 0;
}
## The role of :
## You can combine the symbols on both sides of it into one symbol
#define ADD_TO_SUM(num,value) sum##num+=value
int main()
{
int sum5 = 5;
ADD_TO_SUM(5, 10);
printf("%d\n", sum5);
return 0;
}
(5) Macro parameters with side effects
When macro parameters appear more than once in the macro definition , If the parameter has side effects , Then you may be in danger when using this macro , Lead to unpredictable consequences . The side effect is the permanent effect of expression evaluation .
x + 1;// No side effects
x++;// With side effects
Look at a chestnut :
#define MAX(a,b) ((a)>(b)?(a):(b))
int main()
{
int p = 3;
int q = 25;
int r = MAX(p++, q++);
printf("p=%d q=%d r=%d\n", p, q, r);
return 0;
}
(6) Macro and function comparison
The advantages of macro :
a. Macros are better than functions in terms of program size and speed
b. Macros apply to integer 、 Long integer 、 Floating point, etc
The disadvantages of macro :
a. It is possible to significantly increase the length of the program
b. Macros can't be debugged
c. Macro is type independent , It's not rigorous enough
d. Macros can cause operator priority problems
Macro parameters can also appear types :
#define MALLOC(num,type) (type*)malloc(num*sizeof(type))
int main()
{
int* p = MALLOC(10, int);
if (p == NULL)
{
return 1;
}
free(p);
p = NULL;
return 0;
}
|
|
| |||
|
|
| |||
|
|
| |||
|
|
| |||
|
|
| |||
|
|
| |||
|
|
| |||
|
|
|
(7) Naming conventions
All macro names are capitalized
3、#undef
#undef Used to remove a macro definition
4、 Command line definition
many C Our compiler provides a capability , Allows symbols to be defined on the command line , Used to start the compilation process . Suppose an array of a certain length is declared in a program , If the machine memory is limited , We need a very small array , But the other machine's memory is uppercase , We need an array that can be capitalized .
5、 Conditional compilation
Conditional compilation instructions can compile or discard a statement .
Common conditional compilation instructions :
(1)
#if Constant expression
//……
#endif
// Constant expressions are evaluated by the preprocessor
#if 0//0 Said the false ,#if And #endif The statements between are not executed ; If true, execute
int main()
{
printf("money\n");
return 0;
}
#endif
(2)
Conditional compilation of multiple branches
#if Constant expression
//……
#elif Constant expression
//……
#else
//……
#endif
#define M 6
int main()
{
#if M<5
printf("have not money\n");
#elif M==5
printf("have not money\n");
#else
printf("have money\n");
#endif
return 0;
}
(3)
Judge whether it is defined
#if defined(symbol)
sentence
#endif
#define MAX 100
int main()
{
#if defined(MAX)// If MAX Defined by macro , Then execute the following statement
printf("max\n");
#endif
return 0;
}
#ifdef symbol
sentence
#endif
#define MAX 100
int main()
{
#ifdef MAX// If MAX Defined by macro , Then execute the following statement
printf("max\n");
#endif
return 0;
}
#if !defined(symbol)
sentence
#endif
#define MAX 100
int main()
{
#if !defined(MAX)// If MAX Defined by macro , Then the following statement will not be executed
printf("max\n");
#endif
return 0;
}
#ifndef symbol
sentence
#endif
#define MAX 100
int main()
{
#ifndef MAX// If MAX Defined by macro , Then the following statement will not be executed
printf("max\n");
#endif
return 0;
}
(4)
Nested instruction
#if defined(OS_UNIX)
#ifdef OPTION1
unix_version_option1();
#endif
#ifdef OPTION2
unix_version_option2();
#endif
#elif defined(OS_MSDOS)
#ifdef OPTION2
msdos_version_option2();
#endif
#endif
6、 File contains
We already know , #include Instruction can cause another file to be compiled . It's like it actually appears in #include It's the same place as the command . It's a simple alternative : The preprocessor first removes this instruction , And replace... With the contents of the containing file . Such a source file is contained 10 Time , So it's actually compiled 10 Time .
(1) How header files are included
The local file contains :
#include "filename"
First, find the source file in the directory , If the header file is not found , The compiler looks up the library function header file just like it does
Locate the header file .
Linux The path to the standard header file of the environment :
/usr/include
VS The path to the standard header file of the environment :
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
The library file contains :
#include <filename.h>
Go directly to the standard path to find .
(2) Nested files contain
As shown in the figure above , There will be two comm.h The content of , Thus causing file duplication .
Conditional compilation should be used to solve :
// The beginning of each header file is written :
#ifndef __TEST_H__
#define __TEST_H__
// Content of header file
#endif //__TEST_H__
// Or write :
#pragma once
This sharing ends , There will be continuous updates in the future , Don't forget to click three times ~
边栏推荐
- 【学习笔记】“STL演讲比赛流程管理系统”作业总结
- 堆堆排序及堆的相关操作
- 基于STM32F030的ADC功能实现
- 字符函数和字符串函数
- 【学习笔记】“机房预约系统”作业复盘及问题总结
- ZABBIX server Ping link monitoring, alarm by email after status change
- 第五十九篇:main.c:62:9: note: use option -std=c99 or -std=gnu99 to compile your code
- Pytorch target detection coco API explanation data generation
- Yolo series target detection data set
- Hualu Cup - Jiangsu illegal advertising detection - champion summary
猜你喜欢
随机推荐
Student achievement management system (C language)
数塔问题及变形
基于STM32F103,用蜂鸣器播放歌曲
Yolov5 bird detection
yolov3的GUI界面(3)--解决out of memory问题,新增摄像头检测功能
动态内存管理
标准IO与文件IO
Data tower problem and deformation
Share what you learned this week - detailed explanation of transformer model
Pytorch mmdetection2.0 installation training test (coco training set)
Zabbix Server Ping链路监控,状态改变后通过邮件告警
更易上手的C语言入门级芝士 (3) 常见关键字+define+指针+结构体(超详细)
YOLOv5飞鸟检测
YOLOv5飛鳥檢測
指针数组和数组指针有什么区别?
ZABBIX server Ping link monitoring, alarm by email after status change
Pytorch target detection data enhances the mixing of cutmix and mixup
【学习笔记】Solid Works 作业记录
yolov3的权重文件和预训练文件
什么是多线程