C Program to Store Information of Students Using Structure

In this example, we'll define a structure to represent a student's information, and then we'll demonstrate how to create an array of structures to store multiple students' data. Finally, we'll show how to retrieve and display the information.

#include <stdio.h>

// Define a structure to store student information
struct Student {
    int rollNumber;
    char name[50];
    int age;
    float marks;
};

int main() {
    int numStudents;

    printf("Enter the number of students: ");
    scanf("%d", &numStudents);

    // Declare an array of structures to store student information
    struct Student students[numStudents];

    // Input student information
    for (int i = 0; i < numStudents; i++) {
        printf("\nEnter information for Student %d:\n", i + 1);
        printf("Enter Roll Number: ");
        scanf("%d", &students[i].rollNumber);
        printf("Enter Name: ");
        scanf(" %[^\n]", students[i].name);
        printf("Enter Age: ");
        scanf("%d", &students[i].age);
        printf("Enter Marks: ");
        scanf("%f", &students[i].marks);
    }

    // Display student information
    printf("\nStudent Information:\n");
    for (int i = 0; i < numStudents; i++) {
        printf("Student %d\n", i + 1);
        printf("Roll Number: %d\n", students[i].rollNumber);
        printf("Name: %s\n", students[i].name);
        printf("Age: %d\n", students[i].age);
        printf("Marks: %.2f\n\n", students[i].marks);
    }

    return 0;
}
 

In this program, we first define a structure named Student, which contains the following fields:

  • rollNumber: An integer to store the student's roll number.
  • name: A character array (string) to store the student's name.
  • age: An integer to store the student's age.
  • marks: A floating-point number to store the student's marks.

The program starts by asking the user to enter the number of students for which they want to store information. It then declares an array of Student structures with a size equal to the number of students entered by the user.

The program uses a loop to input student information for each student, and the data is stored in the respective structure elements. The scanf function is used for input, and it is essential to use " %[^\n]" for reading the name to handle spaces.

Finally, the program displays the stored student information by iterating through the array of structures and printing the details for each student.

Example Input:

Enter the number of students: 3

Enter information for Student 1:
Enter Roll Number: 101
Enter Name: John Smith
Enter Age: 20
Enter Marks: 95.5

Enter information for Student 2:
Enter Roll Number: 102
Enter Name: Alice Johnson
Enter Age: 21
Enter Marks: 88.0

Enter information for Student 3:
Enter Roll Number: 103
Enter Name: Bob White
Enter Age: 19
Enter Marks: 75.5 

Example Output:

Student Information:
Student 1
Roll Number: 101
Name: John Smith
Age: 20
Marks: 95.50

Student 2
Roll Number: 102
Name: Alice Johnson
Age: 21
Marks: 88.00

Student 3
Roll Number: 103
Name: Bob White
Age: 19
Marks: 75.50 

This C program allows you to store and display the information of multiple students using a structure.

 

Prasun Barua

Prasun Barua is an Engineer (Electrical & Electronic) and Member of the European Energy Centre (EEC). His first published book Green Planet is all about green technologies and science. His other published books are Solar PV System Design and Technology, Electricity from Renewable Energy, Tech Know Solar PV System, C Coding Practice, AI and Robotics Overview, Robotics and Artificial Intelligence, Know How Solar PV System, Know The Product, Solar PV Technology Overview, Home Appliances Overview, Tech Know Solar PV System, C Programming Practice, etc. These books are available at Google Books, Google Play, Amazon and other platforms.

*

Post a Comment (0)
Previous Post Next Post