Open In App

C Program to Store Information of Students Using Structure

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.  The struct keyword is used to define the structure in the C programming language. The items in the structure are called its member and they can be of any valid data type.

Syntax:

struct structure_name {
   data_type member_name1;
   data_type member_name1;
   ....
   ....
};

Program Store Information of Students Using Structure

Write a C program to store the information of Students using Structure. The information of each student to be stored is:

Each Student Record should have:
Name
Roll Number
Age
Total Marks

The ‘struct’ keyword is used to create the student structure as:

struct Student
{ 
   char* name; 
   int roll_number;
   int age;
   double total_marks;
};
  • Get the number of Students whose details are to be stored. Here we are taking 5 students for simplicity.
  • Create a variable of Student structure to access the records. Here it is taken as a ‘student’
  • Get the data of n students and store it in student fields with the help of the dot (.) operator 

Syntax:

student[i].member = value;

After all the data is stored, print the records of each student using the dot (.) operator and loop. 

Syntax:

student[i].member;

Below is the implementation of the above approach: 

C




// C Program to Store Information
// of Students Using Structure
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
// Create the student structure
struct Student {
    char* name;
    int roll_number;
    int age;
    double total_marks;
};
 
// Driver code
int main()
{
    int i = 0, n = 5;
 
    // Create the student's structure variable
    // with n Student's records
    struct Student student[n];
 
    // Get the students data
    student[0].roll_number = 1;
    student[0].name = "Geeks1";
    student[0].age = 12;
    student[0].total_marks = 78.50;
 
    student[1].roll_number = 5;
    student[1].name = "Geeks5";
    student[1].age = 10;
    student[1].total_marks = 56.84;
 
    student[2].roll_number = 2;
    student[2].name = "Geeks2";
    student[2].age = 11;
    student[2].total_marks = 87.94;
 
    student[3].roll_number = 4;
    student[3].name = "Geeks4";
    student[3].age = 12;
    student[3].total_marks = 89.78;
 
    student[4].roll_number = 3;
    student[4].name = "Geeks3";
    student[4].age = 13;
    student[4].total_marks = 78.55;
 
    // Print the Students information
    printf("Student Records:\n\n");
    for (i = 0; i < n; i++)
    {
        printf("\tName = %s\n", student[i].name);
        printf("\tRoll Number = %d\n", student[i].roll_number);
        printf("\tAge = %d\n", student[i].age);
        printf("\tTotal Marks = %0.2f\n\n", student[i].total_marks);
    }
 
    return 0;
}


Output:

Student Records:

    Name = Geeks1
    Roll Number = 1
    Age = 12
    Total Marks = 78.50

    Name = Geeks5
    Roll Number = 5
    Age = 10
    Total Marks = 56.84

    Name = Geeks2
    Roll Number = 2
    Age = 11
    Total Marks = 87.94

    Name = Geeks4
    Roll Number = 4
    Age = 12
    Total Marks = 89.78

    Name = Geeks3
    Roll Number = 3
    Age = 13
    Total Marks = 78.55


Last Updated : 01 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads