当前位置:网站首页>Program environment and pretreatment
Program environment and pretreatment
2022-07-23 09:25:00 【Michael byte】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
1. The translation environment of the program
2. The execution environment of the program
3. Detailed explanation :
c Compilation of language programs + link
4. Explanation of predefined symbols
5. Preprocessing instruction #define
6. The comparison between macro and function
7. Preprocessing operators # and ## Introduction to
8. Command definition
9. Preprocessing instruction #include
10 Preprocessing instruction #undef
11. Conditional compilation
1. Compilation and linking of programs

3. Pretreatment details
3.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
#include<stdio.h>
int main()
{
printf("line:%d,date:%s,time:%s", __LINE__, __DATE__, __TIME__);
return 0;
}
3.2
grammar :
#define name stuff
for instance :
#define MAX 1000
#define reg register
#define do_forever for(;;)
#define CASE break;case
#define bug printf("line:%d\t \
date:%s\ttime:%s\n",\
__FILE__,__LINE__, \
__DATE__,__TIME__)



3.2#define Defining macro
Macro declaration form
#define name( Parameters )stuff
Be careful :
The left parenthesis of the argument list must be the same as name Next door neighbor
If there is any gap between the two , The parameter list will be interpreted as stuff Part of
#define double(x) x+x
#include<stdio.h>
int main()
{
printf("%d", double(2));
return 0;
}Again :
#define SQUARE(x) x*x
If after the above statement , You put
SOUARE(5);
Put in the program , The preprocessor will replace the above expression with the following expression
5*5
There are some problems with this macro :
Observe the following code snippet
int a=5;
printf("%d\n",SQUARE(a+1))At first glance , You may think this paragraph is printed 36 This value , In fact, he can print 11.
Actually, it's because of the parameters x Replaced with a+1, It actually became printf("%d\n",a+1*a+1),
So you need to add two parentheses to the macro definition , In this way, the problem can be solved easily
#define SQUARE(x) (x)*(x)
printf("%d\n",(a+1)*(a+1));
There is also a macro definition
#define DQUBLE(x) (x)+(x)
We use parentheses in the definition , However, new errors may appear in this macro :
int a=5;
printf("%d\n",10*DOUBLE(a));
This question looks like printing 100, But what is actually printed is 55.
We found that after the replacement :
printf("%d\n",10*(5)+(5));
The multiplication operation first adds with the macro definition , So it's here 55. So we need to add brackets
#define DOUBLE(x) ((x)+(x)), When writing macros in the future, you should put parentheses in this way , Avoid the unexpected consequences of operator priority .
3.2.4 ##
## Its function is to combine the symbols on both sides of it into one symbol
#define hu(class,num) class##num
#include<stdio.h>
int main()
{
int class106=100;
printf("%d",hu(class,106));
return 0;
}

3.2.5 Macro parameters with side effects
#define MAX(x,y) ((x)>(y)?(x):(y))
#include<stdio.h>
int main()
{
int a = 2;
int b = 3;
int c = MAX(a++, b++);
printf("%d", c);
printf("%d%d", a, b);
return 0;
} 
So why is it such a result ? We can get (a++)>(b++)?(a++):(b++) First ( a++)>(b++)?a<b, then a++,b++,a b The values of are 3,4, Then return b++,c Is equal to b be equal to 4, then b++,b=5.
3.2.6 Macro and function comparison
Macros are often used to perform simple calculations , For example, find the larger of the two numbers .
#define MAX(a,b) ((a)>(b)?(a):(b))Then why not use functions to perform this task
1. Macros are faster than functions
2. The parameters of a function must be declared as specific parameters , Macros are good for shaping 、 Long integer 、 floating-point
The disadvantages of macro :
1. Macros cannot be debugged
2. Macros are type independent , It's not rigorous enough
3. Macros can cause operator priority problems
4. Every time you use a macro , A copy of the code defined by the macro is inserted into the program , Unless the macro is short , Otherwise, the length of the program may be greatly increased .
3.3#undef
This instruction is used to remove a macro definition
#define MAX 100
#include<stdio.h>
int main()
{
#undef MAX
printf("%d", MAX);
return 0;
}
3.5 Conditional compilation :
1.#if Constant expression
//
#endif
Such as :
#define__debug__1
#if__debug__
//
#endif

2. Conditional compilation of multiple branches
#if Constant expression
//
#elif Constant expression
//else
//
#endif

3. Judge whether it is defined
#if defined(symbol)
#ifdef symbol
#if !defined(symbol)
#ifndef symbol
#define MAX 100
int main()
{
#ifndef MAX
printf("max\n");
#endif
return 0;
}
int main()
{
#if !defined(MAX)
printf("max\n");
#endif
return 0;
}
#define max 100
#include<stdio.h>
int main()
{
#ifdef max
printf("%d", max);
#endif
return 0;
}
#define max 100
#include<stdio.h>
int main()
{
#if defined ( max)
printf("%d", max);
#endif
return 0;
}
边栏推荐
猜你喜欢
随机推荐
BCG 使用之CBCGPColorDialog控件
Is it safe to buy shares and open an account? Will you lose money?
FPGA出错的积累
复盘:什么是B+树?你知道B+树怎么构建有序表的吗?B+树有什么特点
input 输入完成时的触发事件
给我五分钟,给你一片“云”
服务器内存性能调优
1058 A+B in Hogwarts
为什么使用Well-Architected Framework?
pytorch简单示例汇总
【ManageEngine】网络配置管理的6大必备功能
【C语言】预处理详解
Developers must see | devweekly issue 1: what is time complexity?
Advantages of server hosting, server leasing and virtual machine
BeanSearcher接收数组参数、以及逻辑删除
涨薪神器
【管理篇 / 升级】* 02. 查看升级路径 * FortiGate 防火墙
Wallys/PD-60 802.3AT Input Output802.3AT/AT 85% Efficiency 10/100/1000M GE Surge Protection
Vs Code shortcut key settings
QT显示中文乱码




![[C language] file operation](/img/d3/5e5ce369dd3315089b529cf69b36c0.png)




