c
  1. c-program-to-store-information-of-students-using-structure

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

In C programming, structures provide a convenient way to store complex data types. In this tutorial, we will discuss a program to store information of students using a structure.

Syntax

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

Example

#include <stdio.h>
#include <string.h>

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

int main() {
    s.roll = 1;
    strcpy(s.name, "John");
    s.marks = 85.5;

    printf("Roll: %d\nName: %s\nMarks: %.2f\n", s.roll, s.name, s.marks);

    return 0;
}

Output

Roll: 1
Name: John
Marks: 85.50

Explanation

In this program, we have defined a structure named student that has three members: name, roll, and marks. Then we have created a variable s of type student that will store the student information.

In the main() function, we have assigned values to the roll, name, and marks members of s. We have used the strcpy() function to copy the string "John" to the name member since it is a string. Then we have printed the values of the roll, name, and marks members using the printf() function.

Use

The program to store information of students using a structure can be used to keep track of student records in an organized manner. This program can be easily modified to handle multiple student records by using arrays of structures.

Summary

In this tutorial, we learned how to create a program to store information of students using a structure in the C programming language. We covered the syntax, example, explanation, use, and output of the program. The program can be used to store and maintain student records in an organized manner.

Published on: