C Program to Store Information of Students Using Structure
Last Updated :
21 Aug, 2024
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: 90
Output: 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
// 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
========================================
Similar Reads
C program to store Student records as Structures and Sort them by Age or ID Given studentâs records with each record containing id, name and age of a student. Write a C program to read these records and display them in sorted order by age or id. Sorting by Age Examples: Input: Student Records = { {Id = 1, Name = bd, Age = 12 }, {Id = 2, Name = ba, Age = 10 }, {Id = 3, Name
6 min read
Output of C programs | Set 44 (Structure & Union) Prerequisite: Structure and UnionQUE.1 What is the output of this program? C #include <stdio.h> struct sample { int a = 0; char b = 'A'; float c = 10.5; }; int main() { struct sample s; printf("%d, %c, %f", s.a, s.b, s.c); return 0; } OPTION a) Error b) 0, A, 10.5 c) 0, A, 10.500000
2 min read
How to Pass or Return a Structure To or From a Function in C? A structure is a user-defined data type in C. A structure collects several variables in one place. In a structure, each variable is called a member. The types of data contained in a structure can differ from those in an array (e.g., integer, float, character). Syntax: struct geeksforgeeks { char nam
3 min read
Read/Write Structure From/to a File in C For writing in the file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of the struct. fwrite and fread make tasks easier when you want to write and read blocks of data.Writing Structure to a File using fwriteWe can use fwr
3 min read
Array of Structures vs Array within a Structure in C Both Array of Structures and Array within a Structure in C programming is a combination of arrays and structures but both are used to serve different purposes.Array within a StructureA structure is a data type in C that allows a group of related variables to be treated as a single unit instead of se
5 min read