In this tutorial, you will learn how to use structure to store information of students in C.
Program to store information of students using structure
In this program we have a structure student
with four members name
, rollNum
, address
and marks
.
We have created an array of structure with size 3 (s[3]
), in order to store details of three students. The size of the array can increased or decreased based on the number of students.
We are running two loops in this program, first loop stores the student information entered by user and the second loop prints the student information stored in the structure.
#include <stdio.h>
struct student {
char name[60];
int rollNum;
char address[75];
float marks;
} s[3];
int main() {
int i;
printf("Enter student information:\n");
// getting information
for (i = 0; i < 3; ++i) {
s[i].rollNum = i + 1;
printf("\nFor roll number%d: \n", s[i].rollNum);
printf("Enter Name: ");
scanf("%s", s[i].name);
printf("Enter Address: ");
scanf("%s", s[i].address);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("\nPrint student Information:\n");
// printing information
for (i = 0; i < 3; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].name);
printf("Address: ");
puts(s[i].address);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output:
Enter student information: For roll number1: Enter Name: Chaitanya Enter Address: Noida Enter marks: 95 For roll number2: Enter Name: Ajeet Enter Address: Delhi Enter marks: 97 For roll number3: Enter Name: Harry Enter Address: Agra Enter marks: 96.5 Print student Information: Roll number: 1 First name: Chaitanya Address: Noida Marks: 95.0 Roll number: 2 First name: Ajeet Address: Delhi Marks: 97.0 Roll number: 3 First name: Harry Address: Agra Marks: 96.5