To handle multiple students in your C program, you can use an array of structures. This allows you to store and manage data for multiple students efficiently.
✅ Key Concepts:
- A struct is a custom data type that holds related data.
- An array of structs lets you store multiple instances of the same struct type.
- Use a loop to collect and display data for each student.
✅ Example: Handling Multiple Students
Here’s how you can modify your program to handle up to 10 students:
#include <stdio.h>
#define MAX_STUDENTS 10
// Define the student struct
struct student {
char name[50];
int rollno;
float m1;
float phy;
float graph;
float bcme;
float beee;
};
int main() {
struct student students[MAX_STUDENTS]; // Array of student structs
int n;
// Ask how many students to input
printf("Enter number of students (max %d): ", MAX_STUDENTS);
scanf("%d", &n);
// Ensure the number is within the limit
if (n > MAX_STUDENTS || n <= 0) {
printf("Invalid number of students.\n");
return 1;
}
// Input data for each student
for (int i = 0; i < n; i++) {
printf("\n--- Student %d ---\n", i + 1);
printf("Enter student name: ");
scanf("%*[^\n]"); // Clear any leftover input
scanf("%*c"); // Clear newline from previous input
fgets(students[i].name, sizeof(students[i].name), stdin);
printf("Enter roll number: ");
scanf("%d", &students[i].rollno);
printf("Enter Marks in M1: ");
scanf("%f", &students[i].m1);
printf("Enter marks in Physics: ");
scanf("%f", &students[i].phy);
printf("Enter marks in Graphics: ");
scanf("%f", &students[i].graph);
printf("Enter marks in BCME: ");
scanf("%f", &students[i].bcme);
printf("Enter marks in BEEE: ");
scanf("%f", &students[i].beee);
}
// Display student details
printf("\n---- Student Details ----\n");
for (int i = 0; i < n; i++) {
printf("\n--- Student %d ---\n", i + 1);
printf("Name: %s", students[i].name);
printf("Roll No: %d\n", students[i].rollno);
printf("M1: %.2f\n", students[i].m1);
printf("Physics: %.2f\n", students[i].phy);
printf("Graphics: %.2f\n", students[i].graph);
printf("BCME: %.2f\n", students[i].bcme);
printf("BEEE: %.2f\n", students[i].beee);
}
return 0;
}
🧠 Explanation:
-
struct student students[MAX_STUDENTS];
→ Declares an array of 10 student structs.
-
for (int i = 0; i < n; i++)
→ Loops to input data for n students.
-
scanf("%*[^\n]"); and scanf("%*c");
→ Clears any leftover input (especially useful after using scanf for numbers).
-
fgets(students[i].name, ...)
→ Safely reads the student's name (including spaces).
-
printf("Name: %s", students[i].name);
→ Displays the name of the i-th student.
📌 Notes:
- You can change
MAX_STUDENTS to any number you like.
- This is a basic version. For more advanced features (like dynamic input, file storage, or error checking), you can build upon this.
- Always ensure that the number of students entered is within the array limit.
✅ Example Output (Sample):
Enter number of students (max 10): 2
--- Student 1 ---
Enter student name: Alice
Enter roll number: 101
Enter Marks in M1: 85.5
Enter marks in Physics: 90.0
Enter marks in Graphics: 78.5
Enter marks in BCME: 88.0
Enter marks in BEEE: 92.0
--- Student 2 ---
Enter student name: Bob
Enter roll number: 102
Enter Marks in M1: 76.0
Enter marks in Physics: 85.5
Enter marks in Graphics: 82.0
Enter marks in BCME: 79.0
Enter marks in BEEE: 84.5
---- Student Details ----
--- Student 1 ---
Name: Alice
Roll No: 101
M1: 85.50
Physics: 90.00
Graphics: 78.50
BCME: 88.00
BEEE: 92.00
--- Student 2 ---
Name: Bob
Roll No: 102
M1: 76.00
Physics: 85.50
Graphics: 82.00
BCME: 79.00
BEEE: 84.50
Would you like to extend this to store student data in a file or calculate averages? Let me know! 😊