当前位置:网站首页>Standard C language learning summary 11
Standard C language learning summary 11
2022-08-03 19:10:00 【c7473168】
一,预处理指令
程序员所编写的代码不是真正的标准C代码,需要一段程序翻译成标准C代码,
才能被编译器编译
翻译的过程叫做预处理,负责翻译的程序叫做预处理器,
被翻译的语句叫做预处理指令,以#开头的都是预处理指令
gcc -E code.c 直接显示预处理后的结果
gcc -E code.c -o code.i 生成预处理文件
二,预处理指令的分类
#include 导入头文件/头文件包含
#include <> Find and import header files directly from the system-specified path
#include "" First search from the current working path,找不到再从系统指定路径查找并导入头文件
gcc code.c -I path
编译时指定头文件的加载路径为path,最先从该路径查找
可以通过设置环境变量来修改,增加系统指定头文件加载路径
修改~/.bashrc 终端配置文件
#define 定义宏
宏常量:#define 宏名 常量值
#define MAX 100
其实本质上就是,Where the macro name appears in the code, it is replaced by
对应的常量值
优点:提高代码可扩展性,提高了可读性,提高了安全性,还可以用在case后面
注意:一般宏名全部大写,末尾不要加分号
局部变量,函数名全部小写,全局变量首字母大写,数组arr,指针p
二级指针pp 字符串str
预定义好的宏:
__func__ 获取函数名
__FILE__ 获取文件名
__DATE__ 获取当前日期
__TIME__ 获取当前时间
__LINE__ 获取当前行数 The above are all strings. This is%d
宏函数:其实就是带参数的宏
#define 宏名(参数名) 替换的内容,代码
宏函数不是真正的函数,不检查参数类型,没有传参,
只有表达式的计算结果没有返回值
#define SUM(a,b) a+b
替换过程:
1,先把代码中使用到宏函数的地方替换为宏函数后面的代码 a+b
2,再把宏函数代码中使用到的参数替换为调用者提供的数据 num1+num2
注意:宏函数后面的替换代码不能直接换行,The end of each line needs to be used
通过续行符\来换行
如果有多行代码,可以使用大括号保护代码
宏的二义性:
由于宏代码所处位置,参数位置,优先级问题,
leading to different interpretable results for the same what else can be done
如何避免宏的二义性:
1,给每个参数加小括号,给整个式子加小括号
2,在使用宏函数时,不要提供带自变运算符的参数
注意:容易出选择题
"Common written test interview questions:
#define 与 typedef 的区别?
#define INT int 宏定义
typedef int INT; 类型重定义
INT num;
如果是普通类型,There is no functional difference between them
#define INTP int*
typedef int* INTPP;
INTP p1,p2,p3; //p1是int* p2 p3 是int
INTPP p1,p2,p3; //p1,p2,p3都是int*
宏函数与普通函数的区别?
它们是什么?
宏函数:Macro substitution with arguments,Just code replacement,Just use it like a function,
不是真正的函数
函数:Is a collection of code that has a function,Will be translated into binary instructions to store code segments
函数名就是它的首地址,There is a separate stack memory,命名空间
有什么不一样:
函数: 返回值 类型检查 安全 内存申请,释放 速度慢 函数跳转
宏函数: 运算结果 通用 危险 替换 速度快 代码冗余
注意:调用频繁,Simple functions are suitable for writing macro functions
"
条件编译
根据条件决定哪些代码是否参与最终的编译
版本控制:
#if 条件
#elif 条件
#else
#endif
头文件卫士:
防止头文件被'重复'包含
#ifndef 宏名
#define 宏名
#endif/*宏名*/
宏名:头文件名全部大写,The decimal point is underlined_代替
注意:头文件中必加头文件卫士
调试,判断:
#ifdef 宏名(DEBUG)
如果宏名定义了,则此处代码参与编译
#else
否则,此处参与编译
#endif
gcc code.c -D宏名
可以在编译时定义宏
定义打印调试信息宏函数,是否打印提示信息,取决于是否定义了DEBUG宏
#ifdef DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...)
#endif
打印错误信息的宏函数
#define error(...) printf("%s:%s %d %s:%m %s %s\n",__FILE__,__func__,__LINE__,__VA_ARGS__,__DATE__,__TIME__)
三,头文件
头文件中应该写什么?
问题:头文件可能会被任意的源文件包含,means that the content in the header file will
Exist in multiple object files simultaneously,因此要满足合并内容不能有冲突
重点:Only declaration statements can be written in header files,绝对不能有定义语句
函数声明
全局变量声明
宏常量
宏函数
typedef 类型重定义
结构,联合,枚举的类型声明
头文件的编写原则:
1,为每个.cWrite a document.h文件,.h文件对.c文件的说明
2,如果需要用到某个.c文件中的全局变量,函数,宏等内容时,
只需要把它的头文件导入即可
3,.cThe file also imports its own.h文件,The intent is for declarations to be consistent with definitions
头文件的相互包含:
假设a.h包含了b.h, b.h又包含了a.h此时编译会出错
解决方案:把a.h需要导入b.h的内容和b.h需要导入a.h的内容提取出来
到另一个c.h
错误提示:location type name"xxxx",大概率就是头文件相互包含
边栏推荐
- U-Net生物医学图像分割讲解(Convolutional Networks for BiomedicalImage Segmentation)
- MySQL如何一劳永逸的永久支持输入中文
- 阿里巴巴政委体系-第九章、阿里政委启示录
- pg_memory_barrier_impl in Postgresql and C's volatile
- 微信小程序分享功能
- Alibaba senior experts create a learning architecture from scratch, including Alibaba's internal technology stack PPT, PFD actual combat
- 阿里资深架构师钟华曰:中台战略思想与架构实战;含内部实施手册
- C#将位图旋转90度
- Big guy, who is free to help me to see what the problem is, I just read MySQL source print, and I just came into contact with flink.
- MYSQL误删数据恢复
猜你喜欢

When does MySQL use table locks and when to use row locks?You should know this

阿里巴巴政委体系-第九章、阿里政委启示录

网络协议-TCP、UDP区别及TCP三次握手、四次挥手

丙二醇二乙酸酯(Propylene Glycol Diacetate)

awk语法-02-运算、数组、格式化输出

首届MogDB征文活动开启啦!

WEB 渗透之CSRF

LOL英雄联盟卡顿掉帧问题解决办法 2022年8月1日

Zhong Hua, senior architect of Ali: China-Taiwan strategic thinking and architecture practice; including internal implementation manual
![[笔记]机器学习之前言介绍](/img/69/e2acd3efd5f513c9c32fca701b66c0.png)
[笔记]机器学习之前言介绍
随机推荐
Postgresql-xl global snapshot and GTM code walking (branch line)
WEB 渗透之CSRF
MPLS的简单应用于实验
基于移动GIS的环保生态管理系统
Postgresql source code (65) analysis of the working principle of the new snapshot system Globalvis
力扣解法汇总899-有序队列
2022年7月国产数据库大事记
Big guy, who is free to help me to see what the problem is, I just read MySQL source print, and I just came into contact with flink.
剑指Offer 56.数组中数字出现的次数
Confused!Ali was abused on the one hand, but was fortunate to be promoted to Huawei's technology, and successfully got the offer, with an annual salary of 40w
基于DMS的数仓智能运维服务,知多少?
阿里巴巴政委体系-第九章、阿里政委启示录
[数据集][VOC]老鼠数据集voc格式3001张
开发即时通讯到底需要什么样的技术,需要多久的时间
实时渲染器不止lumion,Chaos Vantage你值得一试
【C语言学习笔记(六)】分支与跳转(if、else、continue、break、switch)
MySQL基础
Execute the mysql script file in the docker mysql container and solve the garbled characters
【统计机器学习】线性回归模型
Mkke:为什么无法从Oracle 11g或12c升级到Oracle 23c?