Write a C Program to enter Student Details using array of structures

By | 20.03.2017

C Program to enter Student Details using array of structures


Write a C Program to enter Student Details using array of structures. Here’s a Simple Program to understand array of structures in C Programming Language.


Array of Structures


Structure is collection of different data type. An object of structure represents a single record in memory, if we want more than one record of structure type, we have to create an array of structure or object.

As we know, an array is a collection of similar type, therefore an array can be of structure type. You can store “n” number of students record by declaring structure variable as ‘struct student record[n]“, where n can be 1000 or 5000 etc.

This program is used to store and access “name, roll no. and marks ”  for many students using array of structures members.


Syntax for declaring structure array : :


struct struct-name
{
datatype var1;
datatype var2;
– – – – – – – – – –
– – – – – – – – – –
datatype varN;
};

struct struct-name obj [ size ];


Accessing Structure Members


To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type.


Below is the source code for C Program to enter Student Details using array of structures which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* Program to understand array of structures*/

#include<stdio.h>

struct student {
                char name[20];
                int rollno;
                float marks;
               };
int main( )
{
        int i,n;
        printf("Enter how many records u want to store :: ");
        scanf("%d",&n);
        struct student stuarr[n];
        printf("Enter name, roll no. and marks Below :: \n");

        for(i=0; i<n; i++)
        {
                printf("\nEnter %d record :: \n",i+1);

                printf("Enter Name :: ");
                scanf("%s",stuarr[i].name);
                printf("Enter RollNo. :: ");
                scanf("%d",&stuarr[i].rollno);
                printf("Enter Marks :: ");
                scanf("%f",&stuarr[i].marks);

        }
        printf("\n\tName\tRollNo\tMarks\t\n");
        for(i=0; i<n; i++)
                printf("\t%s\t%d\t%.2f\t\n", stuarr[i].name, stuarr[i].rollno, stuarr[i].marks);

                return 0;
}

OUTPUT : :


Enter how many records u want to store :: 5
Enter name, roll no. and marks Below ::

Enter 1 record ::
Enter Name :: John
Enter RollNo. :: 1
Enter Marks :: 67

Enter 2 record ::
Enter Name :: Snow
Enter RollNo. :: 2
Enter Marks :: 88

Enter 3 record ::
Enter Name :: Hodor
Enter RollNo. :: 3
Enter Marks :: 55

Enter 4 record ::
Enter Name :: Ramsey
Enter RollNo. :: 4
Enter Marks :: 77

Enter 5 record ::
Enter Name :: Stark
Enter RollNo. :: 5
Enter Marks :: 99

        Name    RollNo  Marks
        John    1       67.00
        Snow    2       88.00
        Hodor   3       55.00
        Ramsey  4       77.00
        Stark   5       99.00

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post.

4.4 23 votes
Article Rating
Category: Arrays Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Gautam Singh

Sir how can I sort according to marks in ascending

kasglc

dill jitt liye got characters dall k