C语言:结构体、联合体、枚举

结构体

不同类型数据的集合

数组是相同类型数据的集合

结构体定义语法
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//定义结构体
struct student {
	char name[200];
	unsigned int age;
	char tel[16];
	float scores[3];
	char sex;
} stu;//初始化结构体方法四

int main(void)
{
	//初始化结构体方法一:
	//struct student stu = { "zhangsan",18,"18112345678",100,200,300,'M'};
	//初始化结构体方法二:
	//struct student stu = { .age = 18, .name="zhangsan",.tel="18112345678",.scores[0]=100,.scores[1]=200,.scores[2]=300,.sex='M'};
	//初始化结构体方法三:
	//struct student stu;
	strcpy(stu.name, "谢允");
	stu.age = 18;
	strcpy(stu.tel, "18112345678");
	stu.scores[0] = 100;
	stu.scores[1] = 100;
	stu.scores[2] = 100;
	stu.sex = 'M';
	printf("姓名:%s\n", stu.name);
	printf("年龄:%u\n", stu.age);
	printf("电话:%s\n", stu.tel);
	printf("成绩:%.1f\t%.1f\t%.1f\n", stu.scores[0],stu.scores[1],stu.scores[2]);
	printf("性别:%s\n", stu.sex == 'M' ? "男" : "女");
	return 0;
}
结构体大小和内存存储结构
//结构体需要根据数据类型进行内存对齐,按最大数据类型分割内存区域
struct student {
	char name[20];//20
	unsigned int age;//4
	char tel[15];//15
	float scores[3];//12
	cha
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值