结构的定义
结构是 C 编程中另一种用户自定义的可用的数据类型,它允许您存储不同类型的数据项。定义结构需要使用struct关键字。
struct Student
{
int sid;
char name[100];
int age;
};
Student是结构体标签,如果想要定义结构体变量,可以有多种方式:
- 在主函数中声明一个变量,同时赋初值。
#include<stdio.h>
struct Student
{
int sid;
char name[100];
int age;
};
int main(void){
struct Student st ={
2022,"zhangsan",20};
printf("%d,%s,%d\n",st.sid,st.name,st.age);
}
- 在定义结构体时,创建一个结构体变量
#include<stdio.h>
#include<string.h>
struct Student
{
int sid;
char name[100];
int age;
} st;
int main