C Program to Store Students Information Using Structure
Students Information Using Structure
On this page we will write C program to store students information using structure . Structure is an user defined data type that can be used to group elements of heterogeneous type into a singe type.This program will help to understand the basics and working of structure data type and C programming.
Store Students Information Using Structure in C
Structure is an user defined data type that can be used to group elements of different type into a single type.We can access the members of structure by using a variable or we can say by creating an object of structure.Structure data type is declared using ‘struct‘ keyword.
Syntax:
struct structure_name { //Statements };
In Student Information we can include:
- Name
- Roll Number
- Age
- Address
- Total Marks
- Date of birth
Example:
struct Student { char* name; int roll_number; int age; double total_marks; };
Program 1:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { char *name; int roll_number; double marks; } s[3]; int main () { int i = 0; s[0].roll_number = 1; s[0].name = "Aashay"; s[0].marks = 89; s[1].roll_number = 2; s[1].name = "Bhavya"; s[1].marks = 77; s[2].roll_number = 3; s[2].name = "Chetan"; s[2].marks = 92; printf ("Student Records:\n\n"); for (i = 0; i < 3; i++) { printf ("\tName = %s\n", s[i].name); printf ("\tRoll Number = %d\n", s[i].roll_number); printf ("\tMarks = %0.2f\n\n", s[i].marks); } return 0; }
Output:
Student Records: Name = Aashay Roll Number = 1 Marks = 89.00 Name = Bhavya Roll Number = 2 Marks = 77.00 Name = Chetan Roll Number = 3 Marks = 92.00
Program 2:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { char *name; int roll_number; int age; double total_marks; }; int main () { int i = 0; struct Student s[4]; s[0].roll_number = 1; s[0].name = "Aman"; s[0].age = 16; s[0].total_marks = 89.50; s[1].roll_number = 7; s[1].name = "Bharat"; s[1].age = 15; s[1].total_marks = 90; s[2].roll_number = 12; s[2].name = "Saksham"; s[2].age = 17; s[2].total_marks = 72.50; s[3].roll_number = 27; s[3].name = "Vivek"; s[3].age = 16; s[3].total_marks = 66; printf ("Student Records:\n\n"); for (i = 0; i < 4; i++) { printf ("\tName = %s\n", s[i].name); printf ("\tRoll Number = %d\n", s[i].roll_number); printf ("\tAge = %d\n", s[i].age); printf ("\tTotal Marks = %0.2f\n\n", s[i].total_marks); } return 0; }
Output:
Student Records: Name = Aman Roll Number = 1 Age = 16 Total Marks = 89.50 Name = Bharat Roll Number = 7 Age = 15 Total Marks = 90.00 Name = Saksham Roll Number = 12 Age = 17 Total Marks = 72.50 Name = Vivek Roll Number = 27 Age = 16 Total Marks = 66.00
- In the above program we created a structure with name student using ‘struct’ Keyword.
- To access the members of structure student we created structure array s[4] which can store the information of 4 students
- using dot(.) operator we can access the members of struct. For eg: s[i].roll_number=1.
- Similarly using for loop we can print all the stored student information.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment