View All C Programs

 Previews Example  View All C Programs Next Example 

Program to Store Information of Students Using Structure in C

In this program, a structure(student) is created which contains name, roll and marks as its data member. Then, an array of structure of 5 elements is created. Then, data(name, roll and marks) for 5 elements is asked to user and stored in array of structure.
#include <stdio.h>
#include <conio.h>
struct student{
    char name[50];
    int roll;
    float marks;
};
 main(){
    struct student s[10];
    int i;
    printf("Enter information of students:\n");
    for(i=0;i<10;++i)
    {
        s[i].roll=i+1;
        printf("\nFor roll number %d\n",s[i].roll);
        printf("Enter name: ");
        scanf("%s",s[i].name);
        printf("Enter marks: ");
        scanf("%f",&s[i].marks);
        printf("\n");
    }
    printf("Displaying information of students:\n\n");
    for(i=0;i<10;++i)
    {
     printf("\nInformation for roll number %d:\n",i+1);
     printf("Name: ");
     puts(s[i].name);
     printf("Marks: %.1f",s[i].marks);
   }
   getch();
}
Result
Enter information of students:

For roll number 29
Enter name: Sachin
Enter marks: 60


For roll number 30
Enter name: Ashwani
Enter marks: 90
.
.


Displaying information of students:

Information for roll number 29:
Name: Sachin
Marks: 60


Information for roll number 30:
Name: Ashwani
Marks: 90
.
.