C Program to Store Information of a Student using Structure
C program to store information of a student using structure.
In this article, you will learn how to store information of a student using structure in the c programming language.
Example
Enter the number of students: 10
Enter the students's informations:
Information for Roll Number: 1
Enter the first name: Student-1
Enter the last name: Test-1
Enter the marks: 50
Information for Roll Number: 2
Enter the first name: Student-2
Enter the last name: Test-2
Enter the marks: 55
.
.
.
Information for Roll Number: 9
Enter the first name: Student-9
Enter the last name: Test-9
Enter the marks: 90
Information for Roll Number: 10
Enter the first name: Student-10
Enter the last name: Test-10
Enter the marks: 95
Display the student's information:
The Roll Number: 1
The First Name: Student-1
The Last Name: Test-1
The Marks: 50.0
The Roll Number: 2
The First Name: Student-2
The Last Name: Test-2
The Marks: 55.0
.
.
.
The Roll Number: 9
The First Name: Student-9
The Last Name: Test-9
The Marks: 90.0
The Roll Number: 10
The First Name: Student-10
The Last Name: Test-10
The Marks: 95.0
You should have knowledge of the following topics in c programming to understand this program:
- C Structure DataType
- C
for
loop statement - C
printf()
function
In this article, we solved this problem in two methods:
Source Code
// C Program to Store Information of a Student using Structure
#include <stdio.h>
struct student {
char first_name[50];
char last_name[50];
int roll_number;
float marks;
} s[100];
int main() {
int x, i;
// x is the number of students
printf("Enter the number of students: ");
scanf("%d", &x);
// It will store the student's information
printf("\nEnter the students's informations:\n");
for (i = 0; i < x; i++) {
s[i].roll_number = i + 1;
printf("\nInformation for Roll Number:\t%d\n", s[i].roll_number);
printf("Enter the first name: ");
scanf("%s", s[i].first_name);
printf("Enter the last name: ");
scanf("%s", s[i].last_name);
printf("Enter the marks: ");
scanf("%f", &s[i].marks);
}
// It will display the student's information
printf("\n\nDisplay the student's information:\n");
for (i = 0; i < x; i++) {
printf("\nThe Roll Number:\t%d\n", i + 1);
printf("The First Name: ");
puts(s[i].first_name);
printf("The Last Name: ");
puts(s[i].last_name);
printf("The Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
Output
Enter the number of students: 10
Enter the students's informations:
Information for Roll Number: 1
Enter the first name: Student-1
Enter the last name: Test-1
Enter the marks: 50
Information for Roll Number: 2
Enter the first name: Student-2
Enter the last name: Test-2
Enter the marks: 55
Information for Roll Number: 3
Enter the first name: Student-3
Enter the last name: Test-3
Enter the marks: 60
Information for Roll Number: 4
Enter the first name: Student-4
Enter the last name: Test-4
Enter the marks: 65
Information for Roll Number: 5
Enter the first name: Student-5
Enter the last name: Test-5
Enter the marks: 70
Information for Roll Number: 6
Enter the first name: Student-6
Enter the last name: Test-6
Enter the marks: 75
Information for Roll Number: 7
Enter the first name: Student-7
Enter the last name: Test-7
Enter the marks: 80
Information for Roll Number: 8
Enter the first name: Student-8
Enter the last name: Test-8
Enter the marks: 85
Information for Roll Number: 9
Enter the first name: Student-9
Enter the last name: Test-9
Enter the marks: 90
Information for Roll Number: 10
Enter the first name: Student-10
Enter the last name: Test-10
Enter the marks: 95
Display the student's information:
The Roll Number: 1
The First Name: Student-1
The Last Name: Test-1
The Marks: 50.0
The Roll Number: 2
The First Name: Student-2
The Last Name: Test-2
The Marks: 55.0
The Roll Number: 3
The First Name: Student-3
The Last Name: Test-3
The Marks: 60.0
The Roll Number: 4
The First Name: Student-4
The Last Name: Test-4
The Marks: 65.0
The Roll Number: 5
The First Name: Student-5
The Last Name: Test-5
The Marks: 70.0
The Roll Number: 6
The First Name: Student-6
The Last Name: Test-6
The Marks: 75.0
The Roll Number: 7
The First Name: Student-7
The Last Name: Test-7
The Marks: 80.0
The Roll Number: 8
The First Name: Student-8
The Last Name: Test-8
The Marks: 85.0
The Roll Number: 9
The First Name: Student-9
The Last Name: Test-9
The Marks: 90.0
The Roll Number: 10
The First Name: Student-10
The Last Name: Test-10
The Marks: 95.0
Explanation
In this program, we asked the user to enter the 10
number of students to add to the student structure variable.
After that, we filled in 10 students' input data like First Name, Last Name, and Marks of the students.
Then applied for
loop statement to make this calculation and It returned the 10 students' records as output.
C Program to Store Information of a Student using Union
// C Program to Store Information of a Student using Union
#include <stdio.h>
union records {
char first_name[50];
char last_name[50];
int roll_number;
float marks;
};
int main() {
int x, i;
// x is the number of students
printf("Enter the number of students: ");
scanf("%d", &x);
// To assign the records into union set
union records student[x];
// It will store the student's information
printf("\nEnter the students's informations:\n");
for (i = 0; i < x; i++) {
student[i].roll_number = (i + 1);
printf("\nInformation for Roll Number:\t%d\n", student[i].roll_number);
printf("Enter the first name: ");
scanf("%s", student[i].first_name);
printf("Enter the last name: ");
scanf("%s", student[i].last_name);
printf("Enter the marks: ");
scanf("%f", &student[i].marks);
}
// It will display the student's information
printf("\n\nDisplay the student's information:\n");
for (i = 0; i < x; i++) {
printf("\nThe Roll Number:\t%d\n", (i + 1));
printf("The First Name: ");
puts(student[i].first_name);
printf("The Last Name: ");
puts(student[i].last_name);
printf("The Marks: %.1f", student[i].marks);
printf("\n");
}
return 0;
}
Output
Enter the number of students: 5
Enter the students's informations:
Information for Roll Number: 1
Enter the first name: Student-1
Enter the last name: Test-1
Enter the marks: 60
Information for Roll Number: 2
Enter the first name: Student-2
Enter the last name: Test-2
Enter the marks: 65
Information for Roll Number: 3
Enter the first name: Student-3
Enter the last name: Test-3
Enter the marks: 70
Information for Roll Number: 4
Enter the first name: Student-4
Enter the last name: Test-4
Enter the marks: 75
Information for Roll Number: 5
Enter the first name: Student-5
Enter the last name: Test-5
Enter the marks: 80
Display the student's information:
The Roll Number: 1
The First Name:
The Last Name:
The Marks: 60.0
The Roll Number: 2
The First Name:
The Last Name:
The Marks: 65.0
The Roll Number: 3
The First Name:
The Last Name:
The Marks: 70.0
The Roll Number: 4
The First Name:
The Last Name:
The Marks: 75.0
The Roll Number: 5
The First Name:
The Last Name:
The Marks: 80.0
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student