当前位置:网站首页>C语言实验十四 结构体
C语言实验十四 结构体
2022-08-03 23:37:00 【Meteor.792】
一、实验目的
1、掌握结构体类型变量的定义和使用;
2、掌握结构体类型数组的概念和使用;
二、实验内容
结构体
C 语言提供了一种如果用简单变量来分别代表属性,难以反映出他们之间的内在联系数据类型称为结构体。如学生姓名、编号、性别、年龄、各科成绩、 地址等。 他们是同一个处理对象,学生的属性,在这之间,即有字符型、也有长整、短整型、实型等各 种数据类型。例:
Num | name | sex | age | score | addr |
10010 | Li fum | m | 18 | 88.5 | beijin |
整型 | 字符型 | 字符 | 整型 | 实型 | 字符型 |
struct student
{ int num;
char name[20];
char sex;
short int age; float score; char addr[30];
}
#include "stdio.h"
void main()
struct student
{ { int num;
char name[20];
char sex;
short int age;
float score;
char addr[30];
} a={10010,"Li fum",'m',18,88.5,"Bei jing"};
printf("num:%d\nname:%s\nsex:%c\nage:%d\nscore:%f\naddr:%s\n",a.num,a.name,a.sex,a.age,a.score,a.addr);
}上面就定义了一个结构体类型, struct 是关键字,结构体类型是 student 。其中有 6 个不同的数据项。
结构体类型不同于基本数据类型的特点: (1)由若干个数据项组成,每个数据项称为一个结构体的成员,也可称为“域”。 (2)结构体类型并非只能有一种,而可以有千千万万。
struct 结构体名
{
成员项表列
};
定义一个结构体类型,并不意味着系统将分配一段内存单元来存放各数据项成员。 因为这仅仅只定义了类型。结构体类型需用户自己定义。
下面是结构体的应用(输出学生的学号、姓名和分数):
#include "stdio.h"
#define N 5
struct student
{
char num[6];
char name[8];
int score[4];
}stu[N];
void main()
{
int i,j;
void print(struct student stu[N]);
for(i=0;i<N;i++)
{
printf("\n输出学生的分数为:%d\n",i+1);
printf("学号:");scanf("%s",stu[i].num);
printf("名字:");scanf("%s",stu[i].name);
for(j=1;j<4;j++)
{
printf("分数%d:",j);
scanf("%d",&stu[i].score[j]);
}
printf("\n");
}
print(stu);
}
void print(struct student stu[N])
{
int i,j ;
printf("\n NO. name score1 score2 score3\n");
for(i=0;i<N;i++)
{
printf("%5s%1 0s",stu[i] .num,stu[i] .name);
for(j=1;j<=3;j++)
printf("%9d",stu[i] .score[j]);
printf("\n");
}
}边栏推荐
猜你喜欢

Jar a key generation document database
![[Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve](/img/17/342676e20fc04ce9083c5ed443bc1d.png)
[Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve

禾匠编译错误记录

ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset

射频芯片(RFIC)的协议之5G及其调制

Software testing is seriously involution, how to improve your competitiveness?

代码随想录笔记_动态规划_416分割等和子集

Creo 9.0二维草图的诊断:加亮开放端点

jav一键生成数据库文档

redis持久化方式
随机推荐
Creo 9.0在草图环境中创建坐标系
(PC+WAP)织梦模板不锈钢类网站
Prometheus监控Harbor(二进制版)
Unity intercepts 3D images and the implementation of picture-in-picture PIP
我的祖国
Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan
ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
The salary of soft testers at each stage, come to Kangkang, how much can you get?
国内首发可视化智能调优平台,小龙带你玩转KeenTune UI
走迷宫 BFS
射频芯片(RFIC)的协议之5G及其调制
ML之yellowbrick:基于titanic泰坦尼克是否获救二分类预测数据集利用yellowbrick对LoR逻辑回归模型实现可解释性(阈值图)案例
YOLOv7改进之二十二:涨点神器——引入递归门控卷积(gnConv)
【RYU】rest_router.py源码解析
Creo 9.0二维草图的诊断:加亮开放端点
SRE运维解密-什么是SRE:DevOps模型的具体实践!
Code Casual Recording Notes_Dynamic Programming_416 Segmentation and Subsetting
RSS订阅微信公众号初探-feed43
Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent
FinClip,助长智能电视更多想象空间