×

C program to Store Information of Students Using Structure

What is the Structure in C?

User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A variety of various data types are stored there.

A structure is a made-up building or a particular grouping of objects or people, especially with several elements.

C programming structures are given below:

Link, Definition, Documentation, Global Declaration Subprogrammes and the Main() function. The main() function must be present in every C programme; the other functions are optional.

Benefits of Structure

Since we can use a single name to describe the entire record, maintaining it is quite simple. We can use a single argument in Structure to pass an entire set of records to any function. To store more records with related types, use an array of Structures.

The program produces a structured student. Three components make up the Structure: names (string), rolls (integer), and marks (float).

In C, a user-defined data type is called a structure. An item with potentially many types can be combined into a single type using the data type that a structure offers.

The student structure is built using the 'struct' keyword as follows:

struct Student

{

char* name;

int roll_number;

int age;

double total_marks;

};

Here is a sample program on how to store information about students and display it using Structure,

// Store information of students and display it using Structure
#include<stdio.h>
#include<conio.h>
Struct student{
char name[50];//declaration of file of char type
int roll;
float marks;
}
int main()
{
printf("Enter information:\n");// requests that the user enter information
printf("Enter name: ");// seeks the user to enter the name
fgets(s,.name,size of(s.name),stdin);// File input


printf("Enter roll number:");// seeks the user to enter the roll number
scanf("%d,&s.roll");// to make loop ends
printf("%f",&s.marks);
printf("Display Information:\n");// Displays the information 
printf("Name: ");// prints the information required
printf("%s",s.name);
printf("Roll number: %d\n",s.roll);// prints the information required
printf("Marks:%1f\n",s.marks);
return 0;
}

Output

Enter information:
Enter name: Bittu
Enter roll number: 025
Enter marks: 86.5
Display Information:
Name: Bittu
Roll number: 25
Marks: 86.500000

The above program is based on only particular student information students using Structure.

Find out how many students' information will be kept on file. We are only taking 5 students in this instance to keep things simple. To access the data, create a Student structure variable. Here, it is assumed to mean "student."

Retrieve the information for n students using the dot (.) operator, then enter it into the fields for each student.

Syntax:

Student[i].member = value;

Use the dot (.) operator and a loop to print the records of each student after all the data has been recorded.

Syntax:

Student[i].member;

/* This program is based on data from five students who used 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;
// Make a variable for the student's Structure.
// with n records for students
struct Student student[n];
// Obtain student information
student[0].roll_number = 1;
student[0].name = "Bittu";
student[0].age =21;
student[0].total_marks = 80.26;
student[1].roll_number = 4;
student[1].name = "Chintu";
student[1].age = 18;
student[1].total_marks = 90.74;
student[2].roll_number = 2;
student[2].name = "Yash";
student[2].age = 11;
student[2].total_marks = 87.54;
student[3].roll_number = 5;
student[3].name = "Sunny";
student[3].age = 12;
student[3].total_marks = 76.39;
student[4].roll_number = 3;
student[4].name = "Sohail";
student[4].age = 13;
student[4].total_marks = 81.22;
// 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 = Bittu
Roll Number = 1
Age = 21
Total Marks = 80.26


Name = Chintu
Roll Number = 4
Age = 18
Total Marks = 90.74


Name = Yash
Roll Number = 2
Age = 11
Total Marks = 87.54


Name = Sunny
Roll Number = 5
Age = 12
Total Marks = 76.39


Name= Sohail
Roll Number = 3
Age = 13
Total Marks = 81.22

Explanation

With the help of structures, this software keeps the data (name, roll, and marks) of 5 students.

This program creates a structure for the students.

Name (string), roll (integer), and marks (marks) are the three components that make up this Structure (float).

The application retrieves the data for 5 students from the user and shows it on the screen using a loop.


Related Topics

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Union in C

Union is a user-defined data type that is used to hold the different types of elements like structure. In union, all members share the same memory location. Syntax: union [union name] { ...

1 minute read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

7 Best IDEs for C/C++ Developers in 2024

As we all know that in programming languages, C is known as the building block which cannot be contradicted, and C++ is the prolong kind of C and it can...

4 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.