Handling student data can be tricky, especially with different data types. That's where C structures come in. You can group various related data types into one name. Let’s see how to store info like name, roll number, and marks all at once.
So, you’ve got two students, Alice and Bob, with their roll numbers and marks. This program takes their details and stores them efficiently using a structure. Here’s how it works.
What You Need Before Writing the C Program to Store Information of Students
Before you start coding, ensure you understand the basics needed for this program.
- Using
structto define a structure in C. - Knowledge of arrays and loops in C.
- Standard I/O functions such as
printfandscanf. - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.
Source Code of C Program to Store Information of Students Using Structure
Here's the complete source code that defines a structure to handle student information.
// Program to store information of students using structure
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
int main() {
int n, i;
struct Student students[10];
// Read the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
// Input the details of each student
for (i = 0; i < n; i++) {
printf("Enter information for student %d:\n", i + 1);
printf("Enter name: ");
scanf("%s", students[i].name);
printf("Enter roll number: ");
scanf("%d", &students[i].roll);
printf("Enter marks: ");
scanf("%f", &students[i].marks);
}
// Print the student details
printf("\nDisplaying information:\n");
for (i = 0; i < n; i++) {
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].roll);
printf("Marks: %.2f\n\n", students[i].marks);
}
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Store Information of Students Program
Here's what you'll see when you run the program. It starts by asking for the number of students, then collects information for each one.
Enter the number of students: 2
Enter information for student 1:
Enter name: Alice
Enter roll number: 101
Enter marks: 89.5
Enter information for student 2:
Enter name: Bob
Enter roll number: 102
Enter marks: 76.0
Displaying information:
Name: Alice
Roll Number: 101
Marks: 89.50
Name: Bob
Roll Number: 102
Marks: 76.00
Algorithm to Store Student Information in C
This program captures and stores student details by leveraging C structures. Below is the algorithm that goes through each required step.
- Define a structure named
Studentwith fields:name,roll, andmarks. - Declare an array of
struct Studentto store multiple students' data. - Input the number of students.
- For each student, get their name, roll number, and marks and store the data in the structure array.
- Finally, print the information for all students.
flowchart TD
A([Start]) --> B[/Define a student structure/]
B --> C["Declare an array of structures"]
C --> D["Set number of students"]
D --> E{"i < number_of_students?"}
E -->|Yes| F[/Get student information/]
F --> G[/Store information in structure/]
G --> H["i++"]
H --> E
E -->|No| I["Print student information"]
I --> J([End])How the C Program to Store Information of Students Works
Each part of the code is crucial to understanding how we manipulate data using structures in C. Let's break it down.
- The program begins by including the
<stdio.h>library for input and output functions. - A
structnamedStudentis defined with fields:name,roll, andmarks. - An array
studentsis created to hold up to 10 student records. - The program prompts the user to enter the number of students and uses a
forloop to gather each student's details. scanfis used to capture input for the name, roll number, and marks fields of each student.- After collecting the data, another
forloop iterates over the array to print each student's information usingprintf. - The program ends with a
return 0, which signifies successful execution.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is the primary use of a structure in this C program?
Time and Space Complexity of the C Program to Store Information of Students
Let's explore the performance of this program in terms of time and space.
Time Complexity: O(n). n is how many students you enter — 2 for Alice and Bob. The program processes input/output operations for each student inside a loop that executes for (i = 0; i < n; i++). So, if you have 10 students, it requires 10 iterations.
Space Complexity: O(n). The space requirement grows with the number of students as we store each one's details in the students array, where every student has three fields: name, roll, and marks.
Common mistake:Not allocating enough memory for the character array can cause buffer overflow.
Key takeaway:C structures are great for grouping related data, making it easier to manage complex datasets like student info.
FAQs About C Program to Store Information of Students Using Structure
These common questions cover everything you might wonder about this program.
students[i].name gets the name of the i-th student.scanf for reading integers and floating-point numbers, respectively.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Add Two Distances in Inch-Feet Using Structures
- C Program to Store Data in Structures Dynamically
- C Program to Add Two Complex Numbers by Passing Structure
- C Program to Swap Two Numbers Using Pointers
- C Program to Find Largest Number Using Dynamic Memory Allocation
Conclusion
Using structures in C for student data makes it easier to manage related information. You can Experiment by adding fields like address or phone number. Structures help simplify handling complex datasets. If you’re curious about dynamic memory allocation, check out linked lists topic on eduseekho.
Practice more with our C quiz or try running different programs right in the online compiler.