C Program to Store Information of Students Using Structure
This program stores the information (name, roll and marks) of 10 students using structures.
In this program, a structure, student is created.
This structure has three members: name (string), roll (integer) and marks (float).
Then, we created a structure array of size 10 to store information of 10 students.
Using for loop, the program takes the information of 10 students from the user and displays it on the screen.
Example: Store Information in Structure and Display it
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s[10];
int main()
{
int i;
printf("Enter information of students:\n");
// storing information
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:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
Output
Enter information of students: For roll number1, Enter name: Tom Enter marks: 98 For roll number2, Enter name: Jerry Enter marks: 89 . . . Displaying Information: Roll number: 1 Name: Tom Marks: 98 . . .
C program that stores information of students using structures:
#include <stdio.h>
struct student {
char name[50];
int roll_no;
float marks;
};
int main() {
struct student s1, s2;
// Input for s1
printf("Enter name of student 1: ");
scanf("%s", s1.name);
printf("Enter roll number of student 1: ");
scanf("%d", &s1.roll_no);
printf("Enter marks of student 1: ");
scanf("%f", &s1.marks);
// Input for s2
printf("Enter name of student 2: ");
scanf("%s", s2.name);
printf("Enter roll number of student 2: ");
scanf("%d", &s2.roll_no);
printf("Enter marks of student 2: ");
scanf("%f", &s2.marks);
// Output
printf("\nStudent 1:\n");
printf("Name: %s\n", s1.name);
printf("Roll number: %d\n", s1.roll_no);
printf("Marks: %.2f\n", s1.marks);
printf("\nStudent 2:\n");
printf("Name: %s\n", s2.name);
printf("Roll number: %d\n", s2.roll_no);
printf("Marks: %.2f\n", s2.marks);
return 0;
}
Explanation:
#include <stdio.h>
includes the standard input-output header file in our program, which provides input and output operations functionality.struct student {...};
defines a structure namedstudent
which has three members:name
,roll_no
, andmarks
.int main()
is the entry point of our program.struct student s1, s2;
declares two variabless1
ands2
of typestruct student
.- The next six statements use
printf()
andscanf()
to input the information of the two students. printf("\nStudent 1:\n");
prints a header for the output of student 1.printf("Name: %s\n", s1.name);
prints the name of student 1 usingprintf()
the%s
format specifier.printf("Roll number: %d\n", s1.roll_no);
prints the roll number of student 1 usingprintf()
the%d
format specifier.printf("Marks: %.2f\n", s1.marks);
prints the marks of student 1 usingprintf()
the%f
format specifier. The.2
in%.2f
specifies that only two decimal places should be printed.- Similar statements are used to output the information of student 2.
When we compile and run this program, it will prompt the user to input the information of two students and then display the information of both students.