Store Student Information Using Structure in C

Exercise:

Write a C Program to store data of students using structure. The program should store and display name and marks in order with roll numbers of five students.

Click Here to View the Solution!
#include <stdio.h>       
struct student {
    char firstName[50];
    int roll;
    float marks;
}  s[10];

int main() {
    int i;
    printf("Enter information of students:\n");
    for (i = 0; i < 5; ++i) {
        s[i].roll = i + 1;
        printf("\nFor roll number%d,\n", s[i].roll);

        printf("Enter first name: ");
        scanf("%s", s[i].firstName);

        printf("Enter marks: ");
        scanf("%f", &s[i].marks);
    }
    printf("********Display Information********\n\n");

    // Displaying information
    for (i = 0; i < 5; ++i) {
        printf("\nRoll number: %d\n", i + 1);
        printf("First name: ");
        puts(s[i].firstName);
        printf("Marks: %.1f", s[i].marks);
        printf("\n");
    }
    return 0;
}
Click Here to View the Output!
Enter information of students:
For roll number1,
Enter first name: James
Enter marks: 95
For roll number2,
Enter first name: Tom
Enter marks: 93
For roll number3,
Enter first name: Ali
Enter marks: 90
For roll number4,
Enter first name: Vandusen
Enter marks: 85
For roll number5,
Enter first name: Elgar
Enter marks: 92
********Display Information********
Roll number: 1
First name: James
Marks: 95.0
Roll number: 2
First name: Tom
Marks: 93.0
Roll number: 3
First name: Ali
Marks: 90.0
Roll number: 4
First name: Vandusen
Marks: 85.0
Roll number: 5
First name: Elgar
Marks: 92.0
Click Here to View the Explanation!
  • This program is used to store and display the student information by using Structures.
  • A Structure student is created which holds three members a string array firstName of the size 50, an integer roll and float type marks.
  • An array of the structures s is then created of the size 5 for storing the data about 5 students.
  • In main(), the program requests the user to enter the information about the students and uses a for loop to take data of 5 students and stores it in the structure array s.
  • Finally, the program uses another for loop that is used to display the data of 5 students from the structure array.
%d bloggers like this: