In C programming, structures help you manage related data efficiently. This tutorial demonstrates how to define a structure student_record
to hold a student’s name, branch, and total marks, and how to read and display the details for 10 students.
Objective
- Define a structure
student_record
with:name
(string)branch
(string)total_marks
(float or int)
- Input details for 10 students.
- Print all student records in a structured format.
Structure Definition: student_record
struct student_record {
char name[50];
char branch[30];
float total_marks;
};
Full C Program Example
#include <stdio.h>
#define SIZE 10
// Define structure
struct student_record {
char name[50];
char branch[30];
float total_marks;
};
int main() {
struct student_record students[SIZE];
// Input student data
for (int i = 0; i < SIZE; i++) {
printf("\nEnter details for Student %d\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Branch: ");
scanf("%s", students[i].branch);
printf("Total Marks: ");
scanf("%f", &students[i].total_marks);
}
// Display student data
printf("\n--- Student Records ---\n");
for (int i = 0; i < SIZE; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Name : %s\n", students[i].name);
printf("Branch : %s\n", students[i].branch);
printf("Total Marks: %.2f\n", students[i].total_marks);
}
return 0;
}
Sample Output
Enter details for Student 1
Name: Ravi
Branch: CSE
Total Marks: 455.75
...
--- Student Records ---
Student 1:
Name : Ravi
Branch : CSE
Total Marks: 455.75
Key Concepts Used
- Structure arrays in C.
- Reading multiple fields using
scanf
. - Iterating with loops for data input/output.
Pro Tips
- Use
fgets()
for names with spaces. - Convert marks to percentage format if needed.
- Create functions for input/output to make the code modular.
Conclusion
By using the student_record
structure, you can group and manage multiple attributes of a student efficiently. This is especially useful for applications like student management systems or academic software.
This program is a great example of how to store and retrieve structured data using arrays of structures in C.