当前位置:网站首页>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);
}
运行结果:

边栏推荐
- In order to counteract the drop in sales and explore the low-end market, Weilai's new brand products are priced as low as 100,000?
- An animation optimization of traditional guide layer animation
- 力扣刷题 每日两题(一)
- 可视化图表设计Cookbook
- Win11怎么禁止软件后台运行?Win11系统禁止应用在后台运行的方法
- PyTorch framework to train linear regression model (CPU and GPU environment)
- 有哪些好用的IT资产管理平台?
- An工具介绍之钢笔工具、铅笔工具与画笔工具
- 来广州找工作有一个多月了,今天终于有着落了,工资7000
- Nanoprobes 金纳米颗粒标记试剂丨1.4 nm Nanogold 标记试剂
猜你喜欢

An animation based button animation combined with basic code

An animation optimization of shape tween and optimization of traditional tweening

Jmeter use

第07章 InnoDB数据存储结构【2.索引及调优篇】【MySQL高级】

365天挑战LeetCode1000题——Day 048 有序队列 脑筋急转弯

苹果终于认清现实,销量成为优先考虑,iPhone14将不涨价

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

Yahoo!Answers - data set

【OpenCV】 书本视图矫正 + 广告屏幕切换 透视变换图像处理

An introduction to the pen tool, pencil tool and brush tool
随机推荐
Nodejs 安装依赖cpnm时,install 出现Error: Cannot find module ‘fs/promises‘
Golang structs & methods
Golang arrays and slices
An动画优化之遮罩层动画
[Microservice] Multi-level cache
Golang 通道 channel
BOM系列之sessionStorage
[OpenCV] Cascade classifier training model
TiFlash 计算层概览
An animation optimization of traditional guide layer animation
VLAN 实验
美国拟对华禁售128层以上NAND Flash制造设备
Nanoprobes EnzMet - 酶金相相关介绍及应用
How to disable software from running in the background in Windows 11?How to prevent apps from running in the background in Windows 11
PyTorch构建神经网络预测气温(数据集对比,CPU与GPU对比)
Real number rounding and writing to file (C language file)
leetcode 11. The container that holds the most water
半导体制造业回流美国?宏碁创始人施振荣:违反垂直分工大趋势
冷链行业商业供应链系统:实现全流程数字化协同,激活企业迸发市场活力
Graphic animation and button animation of an animation basic component