解释用C语言访问结构变量

结构是用户定义的数据类型,用于存储数据的不同数据类型的集合。

结构类似于数组。唯一的区别是数组用于存储相同的数据类型,而结构用于存储不同的数据类型。

关键字struct用于声明结构。

结构内部的变量是结构的成员。

一个结构可以声明如下-

Struct structurename{
   //会员声明
};

示例

以下是用于访问结构变量的C程序-

struct book{
   int pages;
   float price;
   char author[20];
};
#include<stdio.h>
   //Declaring structure//
   struct{
   char name[50];
   int roll;
   float percentage;
   char grade[50];
}s1,s2;
void main(){
   //Reading User I/p//
   printf("输入第一名学生的姓名: ");
   gets(s1.name);
   printf("输入第一名学生的卷号: ");
   scanf("%d",&s1.roll);
   printf("输入第一名学生的平均值: ");
   scanf("%f",&s1.percentage);
   printf("输入第一名学生的成绩状态: ");
   scanf("%s",s1.grade);
   //Printing O/p//
   printf("The name of 1st student is : %s\n",s1.name);
   printf("The roll number of 1st student is : %d\n",s1.roll);
   printf("The average of 1st student is : %f\n",s1.percentage);
   printf("The student 1 grade is : %s and percentage of %f\n",s1.grade,s1.percentage);
}
输出结果

执行以上程序后,将产生以下结果-

enter Name of 1st student: Bhanu
enter Roll number of 1st student: 2
Enter the average of 1st student: 68
Enter grade status of 1st student: A
The name of 1st student is: Bhanu
The roll number of 1st student is: 2
The average of 1st student is: 68.000000
The student 1 grade is: A and percentage of 68.000000