C/C++語言之結構體(struct)最基礎的範例
C/C++語言之結構體(struct)最基礎的範例
資料來源: https://mp.weixin.qq.com/s?__biz=MzA3MTU1MzMzNQ==&mid=2247487076&idx=1&sn=75b4340feb8a8f465fb66d963527d8c0&chksm=9f2a963ca85d1f2a7fb429efeeaacbd0718b931b04ab8490676a985e0eee787c00700dd135a1&scene=126&sessionid=1602654827&key=bf49e72a192178bf1dfc808f28753e4d1c33945feae204836134509ec5029bf3d38e789e0d8ba89647b8d6e1c5b196431e5aa80a7b0e7b273685c2f631e35fa04501c580223d9c5b5afdaca0d560e04cedc011dca6f8e70f8ed69f450a0b3198a6332bea376681c83955d6d961fc641d340c8857fbbd5f43d0213c701fccffad&ascene=1&uin=MjIwODk2NDgxNw%3D%3D&devicetype=Windows+10+x64&version=6300002f&lang=zh_TW&exportkey=AgCh0MzxEPID%2FWJCwIe7kFI%3D&pass_ticket=WWWE7RsCr0rtIWqQisdxR3zziZZ2YDSjAvug%2BvJ1LUir0BnxaFh5IhJ9E1vuiPaN&wx_header=0
基本宣告
struct Info { unsigned long identifier;//学号,用无符号长整数表示 char name[20];//名字,用字符数组表示 unsigned int year;//入学年份,用无符号整数表示 unsigned int years;//学制,用无符号整数表示 };
基本使用
#include <stdio.h> typedef struct Info { unsigned long identifier;//学号,用无符号长整数表示 char name[20];//名字,用字符数组表示 unsigned int year;//入学年份,用无符号整数表示 unsigned int years;//学制,用无符号整数表示 }Student; int main() { /** *在main函数中声明结构体变量 *结构体变量名叫info *struct关键字不能丢 */ struct Info info; Student student= {20191101, "Harris",2019,4};//陣列方式初始化 printf("请输入学生的学号:"); scanf("%d", &info.identifier); printf("请输入学生的姓名:"); scanf("%s", info.name); printf("请输入学生的入学年份:"); scanf("%d", &info.year); printf("请输入学生的学制:"); scanf("%d", &info.years); printf("\n数据录入完毕\n\n"); printf("学号:%d\n姓名:%s\n入学年份:%d\n学制:%d\n毕业时间:%d\n", \ info.identifier, info.name, info.year, info.years, info.year + info.years); printf("学号:%d\n姓名:%s\n入学年份:%d\n学制:%d\n毕业时间:%d\n", \ student.identifier, student.name, student.year, student.years, student.year + student.years); return 0; }