C语言-typedef --给已经有的变量类型起名字

文章介绍了typedef在C语言中的作用,主要用来给已存在的数据类型创建别名,如将`unsignedint`定义为`u8`,简化结构体的使用,如`structStudent`定义为`S`。通过示例展示了typedef如何定义和使用,包括数组类型和结构体类型的typedef,以及在主函数中如何操作这些typedef类型的数据。
摘要由CSDN通过智能技术生成

typedef 作用:一般给已经有的变量类型起名字

typedef unsigned int u8;
typedef int arr[10];
struct Student
{
	int data;
	char name[16];
};
typedef struct Student S;

unsigned int data;

data=10;

typedef unsigned int u8;

u8 data=20;

 可值typedef为了 unsigned int类型名,重新命名为u8

一般配合结构体使用,为了方便不需要写struct

struct Student
{
	int data;
	char name[16];
};
typedef struct Student S;
int main()
{
	S s;
	s.data=10;
	printf("struct data=%d\n",s.data);
}

#include<stdio.h>
#include<stdlib.h>
/*
typedef 给已经有的变量类型起名字,
一般配合结构体使用,为了方便不需要写struct 
*/
typedef unsigned int u8;
typedef int arr[10];

struct Student
{
	int data;
	char name[16];
};
typedef struct Student S;

struct 
{
	int data;
	char name[32];
}demo;

typedef struct
{
	int data;
	char name[32];
}Demo2;

void print(S s)
{
	printf("void function_struct data=%d\n",s.data);
}
int main()
{
	u8 data1=100;
	printf("int_u8_data1=%d\n",data1);
	
	arr a;
	a[0]=1;
	printf("arr[10] a[0]=%d\n",a[0]);
	
	S s;
	s.data=10;
	printf("struct data=%d\n",s.data);
	print(s);
	
	
	demo.data=90;
	printf("demo.data=%d\n",demo.data);
	
	Demo2 d;
	d.data=16;
	printf("Demo2 d.data=%s\n",d.data);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值