当前位置:网站首页>typedef&enum&C数据类型
typedef&enum&C数据类型
2022-06-09 21:17:00 【一个逍遥怪】
enum:
定义格式:
typedef enum DAY
{
MON = 1, TUE, WED, THU, FRI, SAT, SUN
}DAY;
1、 相当于一个集合或一种数据类型,内部定义的是整型(字符)常量,这些常量可以直接使用(所以不同枚举类型的内部常量不可以相同)
2、可以定义枚举类型变量,但是这些变量的取值只能是{}内部定义的常量
3、内部常量默认被赋值为整型,默认第一个为0,后面依次+1
可以自定义数值的大小(如果第二个被赋值0,则第一个也为0,【不要这样用,既然不按照顺序来用了,就都赋值】),也可以赋值字符常量(字符本质也是整数)
4、相当于是#define的一个扩展,一次性定义了好多个常量
5、枚举和宏其实非常类似:宏在预处理阶段将名字替换成对应的值,枚举在编译阶段将名字替换成对应的值。可以将枚举理解为编译阶段的宏。
6、枚举中的标识符({}中的)直接被编译到命令里面,放到代码区,所以不能用&取得它们的地址。
7、定义的结构和结构体差不多!!
总结下来:
有以下主要两个用途;
1、定义多个常量(相当于#define)
2、定义枚举变量,限定变量的值
typedef:
1、typedef有掩饰符号类型的功能(指针和数组)
如:①typedef int INT10[10];
INT10 a, b;//此时a和b就是长度为10的int类型数组
等价于:int a[10], b[10];
②typedef int *P;
P p1,p2;//此时p1和p2 就是int类型数组
等价于:int *p1, *p2;
2、typedef和#define的区别
①可以使用其他类型说明符对宏类型名进行拓展,但typedef定义的类型名不可以
#define INTERGE int
unsigned INTERGE n; //没问题
typedef int INTERGE;
unsigned INTERGE n; //错误,不能在 INTERGE 前面添加 unsigned
②在连续定义几个变量的时候,typedef能保证定义的所有变量均为统一类型,而#define不能保证:
#define PTR_INT int *
PTR_INT p1, p2;//等价于int *p1, p2;
typedef int * PTR_INT
PTR_INT p1, p2;//等价与int *p1, *p2;
总结下来:
typedef的用途:
1、重命名,方便编码(变短、掩饰符号类型)
2、不#define更方便(连续定义多个变量)
【感觉主要就是重命名和掩饰符号类型】
----------小插曲----------------
指针实际上是一个新类型。
char *p
malloc中的(*char)
*号是怎么结合的???(*是从右往左结合的)
C语言类型:
1、基本类型:整型、浮点型
2、枚举类型:只能赋值一次的变量(变量的值还是基本类型)
3、void 类型:类型说明符 void 表明没有可用的值。(函数返回值、参数、void指针)
4、派生类型:指针类型、数组类型、结构体类型、共用体类型和函数类型。
数组类型和结构体类型统称聚合类型。
函数类型是指函数返回值的类型。
-
边栏推荐
- datagridview的基本使用 0526
- laravel 8 数据库定时备份
- 202206007 模拟赛 总结
- spider pi 智能视觉六足机器人自动避障 0604
- spider pi 智能视觉六足机器人 标签识别 ApirlTag标签 0604
- After several twists and turns, how long can the TSDB C-bit of incluxdb last?
- 每日技术分享之EIGRP的负载均衡配置
- Spider PI intelligent vision hexapod robot unpacking 0602
- [code audit] Buu_ [GWCTF 2019]mypassword
- PostgreSQL近期常用的表结构查询语句
猜你喜欢

spider pi 智能视觉六足机器人 vnc连接机器人 0603

datagridview的基本使用 0526

js 强制类型转换 和 隐式类型转换 和 Unicode编码

Scratch Programming flying birds Development Notes 0604

10 common high-frequency business scenarios that trigger IO bottlenecks

Ble link layer air packet format

The attribute inheritance of constructor in Es5 borrows the parent constructor method to inherit the prototype object

Pychart always displays the collecting data solution after entering the debug mode

Load balancing configuration of EIGRP for daily technology sharing

spider pi 智能视觉六足机器人自动避障 0604
随机推荐
spider pi 智能视觉六足机器人自动避障 0604
队列在线程池中的应用
一种RS485串口接口电流传感器开合式卡扣互感器支持modbus总线通讯规约协议
The attribute inheritance of constructor in Es5 borrows the parent constructor method to inherit the prototype object
Mqtt graphical client-mqttx installation and use tutorial
Acitivit's mental journey: activiti6.0 customizes any jump command [including rollback]
Introduction to startup of spider PI intelligent vision hexapod robot 0602
The highest monthly salary is 16K. After surviving the difficult times, we can finally usher in the dawn~
What are the advantages of realizing enterprise digital transformation with odoo?
蓝牙协议分析(7)_BLE连接有关的技术分析
sqlserver2012 不允许修改表结构的问题解决 0607
Modbus protocol and serialport port read / write
Thoughts on "sword finger offer1-32"
ST-Link V2 下载出现:internal command error&Error: Flash Download failed - Target DLL has been cancelled
Design of spectrum analyzer based on nexys3 VHDL board
深入理解 Go Modules 的 go.mod 与 go.sum
Embedded software design (interim summary)
MFC connection database shows no data source name found and no default driver specified
datagridview的基本使用 0526
用Odoo实现企业数字化转型有什么好的?