Store Information of a Student Using Structure in C

Exercise:

Write a C Program to store information of a student using structure. The structure should store student’s first name, roll number and marks.

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

int main() {
    printf("Enter information:\n");
    printf("Enter name: ");
    fgets(s.name, sizeof(s.name), stdin);
    printf("Enter roll number: ");
    scanf("%d", &s.roll);
    printf("Enter marks: ");
    scanf("%f", &s.marks);
    printf("Display Information:\n");
    printf("Name: ");
    printf("%s", s.name);
    printf("Roll number: %d\n", s.roll);
    printf("Marks: %.1f\n", s.marks);
    return 0;
}
Click Here to View the Output!
Enter information:
Enter name: James
Enter roll number: 286
Enter marks: 95
Display Information:
Name: James
Roll number: 286
Marks: 95.0
Click Here to View the Explanation!
  • This program is used store and display all the data of a student by using Structure.
  • A structure student is created that declares its three members a string array name, an integer roll, and float marks. And creates a structure variable s for storing and displaying the information.
  • In main(), the program requests the user to enter the student information by first asking for the student name and stores it in the name variable by calling it from the structure using its variable s as s.name.
  • The program then requests the user to enter roll no and marks of the student and stores them in the variables roll and marks by referencing to the structure variables.
  • Finally, all the student information is displayed using printf and the structure variable s.

%d bloggers like this: