c
  1. c-program-to-store-information-of-a-student-using-structure

Program to Store Information of a Student Using Structure - (C Programs)

In this tutorial, we'll discuss how to create a C program that uses structures to store information about a student, such as name, roll number, and marks.

Syntax

struct student {
    char name[50];
    int roll_number;
    float marks;
};
struct student s;

Example

#include <stdio.h>
struct student {
    char name[50];
    int roll_number;
    float marks;
};
int main() {
    struct student s;
    printf("Enter name: ");
    scanf("%s", s.name);
    printf("Enter roll number: ");
    scanf("%d", &s.roll_number);
    printf("Enter marks: ");
    scanf("%f", &s.marks);
    printf("Name: %s\nRoll number: %d\nMarks: %.2f", s.name, s.roll_number, s.marks);
    return 0;
}

Output

Enter name: John
Enter roll number: 123
Enter marks: 89.5
Name: John
Roll number: 123
Marks: 89.50

Explanation

In the above C program, we define a structure student that contains three members - name, roll_number, and marks. We then create a variable s of type struct student to store information about a single student. In the main() function, we use scanf() to read data from the user and store it in the s variable. Finally, we use printf() to print the data stored in s.

Use

The program to store information of a student using structure can be useful in scenarios where you need to store information about multiple students and display it at a later time. This program can easily be extended to store information about multiple students by using arrays of structures.

Summary

In this tutorial, we discussed how to create a C program that uses structures to store information about a student, such as name, roll number, and marks. We covered the syntax, example, output, explanation, use, and important points of using structures in C programming. By using structures, we can easily store data about a single student or multiple students in an organized and efficient manner.

Published on: