Write a C program to store the information of Students using a Structure.
A structure is a user-defined data type in C that is used to create a data type that can be used to group items of possibly different types into a single type.
Example
Input: Name: John Doe, Roll Number: 101, Marks: 85
Name: Jane Smith, Roll Number: 102, Marks: 90Output: Name: John Doe, Roll Number: 101, Marks: 85
Name: Jane Smith, Roll Number: 102, Marks: 90
Store Information of Students Using Structure in C
The student information generally contains the following data:
- Name
- Roll Number
- Age
- Total Marks
Structure to Store the Student Information
The structure should be able to store the above information for each student. So, we will create a data field for each of this information in the structure:
struct Student {
int roll_number;
int age;
double total_marks;
char name[100];
};
We keep the character array name at the end because in case of buffer overflow, it does not overwrite the other variables' data in the structure.
Note: We can add more data fields in the structure according to our needs.
Implementation
Below is the C program to implement the structure to store the student information and print it:
// C Program to Store Information of Students Using Structure
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Create the student structure
struct Student {
char* name;
int roll_number;
int age;
double total_marks;
};
int main() {
// Create an array of student structure variable with
// 5 Student's records
struct Student students[5];
int n = sizeof(students)/sizeof(struct Student);
// Get the students data
students[0].roll_number = 1;
students[0].name = "Geeks1";
students[0].age = 12;
students[0].total_marks = 78.50;
students[1].roll_number = 5;
students[1].name = "Geeks5";
students[1].age = 10;
students[1].total_marks = 56.84;
students[2].roll_number = 2;
students[2].name = "Geeks2";
students[2].age = 11;
students[2].total_marks = 87.94;
students[3].roll_number = 4;
students[3].name = "Geeks4";
students[3].age = 12;
students[3].total_marks = 89.78;
students[4].roll_number = 3;
students[4].name = "Geeks3";
students[4].age = 13;
students[4].total_marks = 78.55;
// Print the Students information
printf("========================================\n");
printf(" Student Records \n");
printf("========================================\n");
for (int i = 0; i < n; i++) {
printf("\nStudent %d:\n", i + 1);
printf(" Name : %s\n", students[i].name);
printf(" Roll Number : %d\n", students[i].roll_number);
printf(" Age : %d\n", students[i].age);
printf(" Total Marks : %.2f\n", students[i].total_marks);
}
printf("========================================\n");
return 0;
}
Output
========================================
Student Records
========================================
Student 1:
Name : Geeks1
Roll Number : 1
Age : 12
Total Marks : 78.50
Student 2:
Name : Geeks5
Roll Number : 5
Age : 10
Total Marks : 56.84
Student 3:
Name : Geeks2
Roll Number : 2
Age : 11
Total Marks : 87.94
Student 4:
Name : Geeks4
Roll Number : 4
Age : 12
Total Marks : 89.78
Student 5:
Name : Geeks3
Roll Number : 3
Age : 13
Total Marks : 78.55
========================================