当前位置:网站首页>typedef关键字的用法
typedef关键字的用法
2022-08-03 13:10:00 【BSP初级小学僧】
基本定义:
typedef为C语言的关键字,作用是为一种数据类型 定义一个新名字。这里的数据类型包括内部数据类型(int,char等)和自定义的数据类型(struct等)。 在编程中使用typedef目的一般有两个,一个是给变量一个易记且意义明确的新名字,另一个是简化一些比较复杂的类型声明。
用途一:与#define的区别
typedef 行为有点像 #define 宏,用其实际类型替代同义字。不同点是 typedef 在编译时被解释,因此让编译器来应付超越预处理器能力的文本替换。
用途二:减少错误
定义一种类型的别名,而不只是简单的宏替换。可以用作同时声明指针型的多个对象。
typedef struct //关键字typedef
{
int id;
int score;
}Stu_i;//爱称(相当于数据类型)
Stu_i stu01={2202,89},stu02;另外typedef在使用时,可以省略结构体变量名:
typedef struct //关键字typedef可以看到如果没有使用typedef关键字的时候,这个就是一个匿名结构体。
源代码:
#include <stdio.h>
#include <string.h>
void struct_test(void);
int main()
{
struct_test();
return 0;
}
void struct_test(void)
{
typedef struct //关键字typedef
{
int id;
int score;
}Stu_i;//爱称(相当于数据类型)
Stu_i stu01={2202,89},stu02;
printf("%d %d",stu01.id,stu01.score);
printf("\n");
stu02.id=2203;
stu02.score=100;
printf("%d %d",stu02.id,stu02.score);
}
运行结果:

边栏推荐
- Golang sync.WaitGroup
- Golang GMP principle
- Graphic animation and button animation of an animation basic component
- An工具介绍之3D工具
- When Nodejs installation depends on cpnm, the install shows Error: Cannot find module 'fs/promises'
- IDEA的模板(Templates)
- [OpenCV] Cascade classifier training model
- An动画基础之按钮动画与基础代码相结合
- 【蓝桥杯选拔赛真题48】Scratch跳舞机游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
- [Practical skills] APP video tutorial for updating APP in CANFD, I2C, SPI and serial port mode of single-chip bootloader (2022-08-01)
猜你喜欢
![[OpenCV] Book view correction + advertising screen switching Perspective transformation image processing](/img/8e/0e8e4e2b362ebdeb071efbf995fc89.png)
[OpenCV] Book view correction + advertising screen switching Perspective transformation image processing

How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11

冷链行业商业供应链系统:实现全流程数字化协同,激活企业迸发市场活力

升级农企业务运营建设,智慧供应链管理平台打造“共赢生态链”

易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元

GameFi industry down but not out | June Report

MySQL数据表操作实战

Real number rounding and writing to file (C language file)

PyTorch构建分类网络模型(Mnist数据集,全连接神经网络)

类和对象(中上)
随机推荐
Jmeter use
有趣的opencv-记录图片二值化和相似度实现
An基本工具介绍之选择线条工具(包教会)
Golang sync.WaitGroup
Graphic animation and button animation of an animation basic component
超大规模的产业实用语义分割数据集PSSL与预训练模型开源啦!
利用pgsql插件PostGIS 实现地理坐标系数据转换
An动画优化之补间形状与传统补间的优化
厨卫电器行业数字化集采管理系统:优化产业供应结构,实现采购业务流程集中管控
An动画基础之元件的影片剪辑效果
An animation optimization of shape tween and optimization of traditional tweening
sessionStorage of BOM series
中国手机品牌争论谁是国内第一,而它已成为中国手机在海外的代表
Relia Tech活性VEGFR重组蛋白丨小鼠 VEGF120实例展示
Golang interface interface
【深度学习】高效轻量级语义分割综述
An工具介绍之3D工具
An动画优化之传统引导层动画
短视频的头号玩家:抖音产品体验报告
An动画基础之元件的图形动画与按钮动画