编写一个C程序来存储使用Structure的学生信息。每个要存储的学生的信息是:
Each Student Record should have:
Name
Roll Number
Age
Total Marks
- 结构是C / C++中用户定义的数据类型。结构创建一个数据类型,该数据类型可用于将可能不同类型的项目分组为单个类型。
- ‘struct’关键字用于创建学生结构,如下所示:
struct Student { char* name; int roll_number; int age; double total_marks; };
- 获取要存储其详细信息的学生数。为了简单起见,我们在这里招收5名学生。
- 创建一个Student结构变量以访问记录。在这里被视为“学生”
- 获取n个学生的数据,并通过点(。)运算符将其存储在学生的字段中
句法:student[i].member = value;
- 存储所有数据后,使用点(。)运算符打印每个学生的记录并循环。
句法:
student[i].member;
下面是上述方法的实现:
// C Program to Store Information
// of Students Using Structure
#include
#include
#include
// Create the student structure
struct Student {
char* name;
int roll_number;
int age;
double total_marks;
};
// Driver code
int main()
{
int i = 0, n = 5;
// Create the student's structure variable
// with n Student's records
struct Student student[n];
// Get the students data
student[0].roll_number = 1;
student[0].name = "Geeks1";
student[0].age = 12;
student[0].total_marks = 78.50;
student[1].roll_number = 5;
student[1].name = "Geeks5";
student[1].age = 10;
student[1].total_marks = 56.84;
student[2].roll_number = 2;
student[2].name = "Geeks2";
student[2].age = 11;
student[2].total_marks = 87.94;
student[3].roll_number = 4;
student[3].name = "Geeks4";
student[3].age = 12;
student[3].total_marks = 89.78;
student[4].roll_number = 3;
student[4].name = "Geeks3";
student[4].age = 13;
student[4].total_marks = 78.55;
// Print the Students information
printf("Student Records:\n\n");
for (i = 0; i < n; i++) {
printf("\tName = %s\n", student[i].name);
printf("\tRoll Number = %d\n", student[i].roll_number);
printf("\tAge = %d\n", student[i].age);
printf("\tTotal Marks = %0.2f\n\n", student[i].total_marks);
}
return 0;
}
输出:
Student Records:
Name = Geeks1
Roll Number = 1
Age = 12
Total Marks = 78.50
Name = Geeks5
Roll Number = 5
Age = 10
Total Marks = 56.84
Name = Geeks2
Roll Number = 2
Age = 11
Total Marks = 87.94
Name = Geeks4
Roll Number = 4
Age = 12
Total Marks = 89.78
Name = Geeks3
Roll Number = 3
Age = 13
Total Marks = 78.55
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。