当前位置:网站首页>程序环境和预处理
程序环境和预处理
2022-07-23 01:25:00 【Michael byte】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
1.程序的翻译环境
2.程序的执行环境
3.详解:
c语言程序的编译+链接
4.预定义符号的详解
5.预处理指令#define
6.宏和函数的对比
7.预处理操作符#和##的介绍
8.命令定义
9.预处理指令#include
10预处理指令#undef
11.条件编译
1.程序的编译和链接

3.预处理详解
3.1预定义符号
__FILE __ //进行编译的源文件
__LINE_ _ //文件当前的行号
__DATE_ _ //文件被编译的日期
__TIME_ _ //文件被编译的时间
__STDC_ _//如果编译器遵循ANSI C,其值为1,否则未定义
#include<stdio.h>
int main()
{
printf("line:%d,date:%s,time:%s", __LINE__, __DATE__, __TIME__);
return 0;
}
3.2
语法:
#define name stuff
举个例子:
#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 定义宏
宏的声明形式
#define name(参数)stuff
注意:
参数列表的左括号必须与name紧邻
如果两者之间有任何空白存在,参数列表就会被解释为stuff的一部分
#define double(x) x+x
#include<stdio.h>
int main()
{
printf("%d", double(2));
return 0;
}再如:
#define SQUARE(x) x*x
如果再上述声明之后,你把
SOUARE(5);
置于程序中,预处理器就会用下面的表达式替换上面的表达式
5*5
这个宏存在一定的问题:
观察下面代码段
int a=5;
printf("%d\n",SQUARE(a+1))乍一看,你可能会觉得这段打印36这个值,而实际上他会打印11.
实际上是因为参数x被替换成a+1,实际上成为了 printf("%d\n",a+1*a+1),
所以需要在宏定义上加上两个括号,这样问题就轻松解决了
#define SQUARE(x) (x)*(x)
printf("%d\n",(a+1)*(a+1));
这里还有一个宏定义
#define DQUBLE(x) (x)+(x)
定义中我们使用了括号,但是这个宏可能会出现新的错误:
int a=5;
printf("%d\n",10*DOUBLE(a));
这个题看上去好像打印100,但实际上打印的是55.
我们发现替换之后:
printf("%d\n",10*(5)+(5));
乘法运算先与宏定义的加法,所以出现了55.所以我们需要再加括号
#define DOUBLE(x) ((x)+(x)),以后再写宏的时候都要以这种方式加括号,避免运算符优先级带来的不可预料的后果。
3.2.4 ##
##的作用是可以把位于它两边的符号合成一个符号
#define hu(class,num) class##num
#include<stdio.h>
int main()
{
int class106=100;
printf("%d",hu(class,106));
return 0;
}

3.2.5带副作用的宏参数
#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;
} 
那么为什么会是这样一个结果呢?我们首先可以得到 (a++)>(b++)?(a++):(b++)首先 ( a++)>(b++)?a<b,然后a++,b++,a b的值分别为3,4,然后返回b++,c就等于b等于4,然后b++,b=5.
3.2.6 宏和函数对比
宏通常被应用于执行简单的计算,比如在两个数中找出较大的一个。
#define MAX(a,b) ((a)>(b)?(a):(b))那为什么不用函数来执行这个任务
1.宏比函数在速度上更胜一筹
2.函数的参数必须声明为特定的参数,而宏适用于整形、长整型、浮点型
宏的缺点:
1.宏没法调试
2.宏与类型无关,也就不够严谨
3.宏可能带来运算符优先级的问题
4.每次使用宏时,一份宏定义的代码插入到程序中,除非宏比较短,否则可能大幅度增加程序的长度。
3.3#undef
这条指令用于移除一个宏定义
#define MAX 100
#include<stdio.h>
int main()
{
#undef MAX
printf("%d", MAX);
return 0;
}
3.5条件编译:
1.#if 常量表达式
//
#endif
如:
#define__debug__1
#if__debug__
//
#endif

2.多个分支的条件编译
#if 常量表达式
//
#elif 常量表达式
//else
//
#endif

3.判断是否被定义
#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;
}
边栏推荐
- transformer汇总
- In the era of Internet of everything, see how IOT test meets the challenge of "core"
- Advantages of implementing automatic network performance monitoring
- 涨薪神器
- 【ManageEngine】网络配置管理的6大必备功能
- SQL用户表的通用设计
- Unity3d learning note 9 - loading textures
- 网站建设开始前要考虑的7个问题
- Amplitude limiting and deblocking filtering of botu PLC signal processing series
- Learn the distributed architecture notes sorted out by Alibaba in one month
猜你喜欢

BGP机房的优点

What is the combined effect of compose and recyclerview?
银联最新测试工程师笔试题目,你能得多少分?

VS2022中出现const char* 无法赋值 char*

【微信小程序】开发入门篇(二)

Airserver third party projection software v7.3.0 Chinese Version (airplay terminal utility)

如何高效系统学习 MySQL?

Advantages of implementing automatic network performance monitoring
![[wechat applet] Introduction to development (2)](/img/8c/92526faee083cd12562fd6adf7efc4.png)
[wechat applet] Introduction to development (2)

VS Code快捷键设置
随机推荐
关系表达式 大于> 小于< 全等=== Nan isNan() 逻辑运算符 双感叹号!! && || % ++ -- 短路计算 赋值表达式 快捷运算符 顺序 闰年
TP5框架 之链接推广项目
Advantages of implementing automatic network performance monitoring
【FPGA教程案例36】通信案例6——基于vivado核的FFT傅里叶变换开发以及verilog输入时序配置详解,通过matlab进行辅助验证
C language classic exercise (1) - "daffodil number"“
Notifyicondata tray used by BCG
Advantages of server hosting, server leasing and virtual machine
一文了解微服务低代码实现方式
提升从改变开始...
1646. Recursive method of getting the maximum value in the generated array
Flutter linear layout, filling
rust allow dead_code
Unity3d learning note 9 - loading textures
Internet download manager is simply a killer of downloaders
砥砺前行新征程,城链科技狂欢庆典在厦门隆重举行
Learn the distributed architecture notes sorted out by Alibaba in one month
TFW6524完美替代进口PT6524芯片方案简介
Summary of some open source libraries that drive MCU hardware debugger (including stlink debugger)
Software testing interview ideas, skills and methods to share, learn is to earn
opensmile简介和安装过程中遇到的问题记录