C program to store information of 10 students using structure

In this tutorial, we write a c program to store information of 10 students using structure. The information contains the name, roll number, marks, and city of 10 students.

 

C program to store information of 10 students using structure

 

You will learn how to store student information in structure and display it on the console as the output.

 

Let’s write a program below is the code you can change the same according to your requirement –

 

Store 10 student information in structure using C-programming

The structure in C-programming is defined using the “struct” keyword like below –

syntax –

struct structureName {
dataType member1;
dataType member2;
dataType member3;
dataType member4;
....
};

Example –

#include <stdio.h>
struct student {
char name[30];
int roll;
int marks;
char city[30];
} stu;

int main() {
printf("Please enter below information:\n");

printf("Enter student name: ");
fgets(stu.name, sizeof(stu.name), stdin);

printf("Enter student roll number: ");
scanf("%d", &stu.roll);

printf("Enter student marks: ");
scanf("%f", &stu.marks);

printf("Enter student city: ");
fgets(stu.city, sizeof(stu.city), stdin);

printf("Displaying student Information:\n");
printf("Name: ");
printf("%s", stu.name);
printf("Roll number: %d\n", stu.roll);
printf("Marks: %.1f\n", stu.marks);
printf("City: ");
printf("%s", stu.city);
return 0;
}

Output –

Please enter below information
Enter student name: Mark
Enter student roll number: 2000
Enter student marks: 70
Enter student city: New York

Displaying student Information:
Name:Mark
Roll number: 2000
Marks: 70
City: New York

The structure student was created in this program that stores the name, roll number, marks, and city information of only one student. But we need to store the information of multiple students let say 10 students so what should we do? To store multiple student information use the same code inside a loop see below example –

 

C program to store information of 10 students using a structure

Example –

#include <stdio.h>
struct student {
char name[30];
int roll;
int marks;
char city[30];
} stu[10];

int main() {
printf("Please enter below information:\n");
int i;

for (i = 0; i < 10; ++i) { 
 printf("Enter student name: ");
 fgets(stu[i].name, sizeof(stu[i].name), stdin);

 printf("Enter student roll number: ");
 scanf("%d", &stu[i].roll);

 printf("Enter student marks: ");
 scanf("%f", &stu[i].marks);

 printf("Enter student city: ");
 fgets(stu[i].city, sizeof(stu[i].city), stdin);
 printf("\n");

}

printf("Displaying student Information:\n");
for (i = 0; i < 10; ++i) { 
 printf("Name: ");
 printf("%s", stu[i].name);
 printf("Roll number: %d\n", stu[i].roll);
 printf("Marks: %.1f\n", stu[i].marks);
 printf("City: ");
 printf("%s", stu[i].city);
}
return 0;
}

 

The for loop will execute 10 times and take the student’s information as input and display the output accordingly.

Conclusion

This is how you can write a c program to store information of 10 students using structure.