LAST UPDATED: JANUARY 10, 2022
Store College Students Information Using A Structure
In C/C++, a structure is a user-defined data type. A structure provides a data type that can be used to combine objects of different types into a single type.
How To Declare Structure In C Programming?
The keyword "struct" is used to specify a structure. "struct" is a new data type that is a collection of various data types. Structure variables can be defined either as part of the structure declaration or as a separate declaration, similar to basic types.
What Is The Syntax Of Structure?
The syntax of structure in c programming is:
struct structure_name
{
declaration;
statement;
};
How Will You Access Structure Elements?
The elements from the structure can be accessed using (.) Dot Operator.
Programm To Store Students Information Using Structure:
#include <stdio.h>
struct student {
char first_name[50];
char last_name[50];
int roll_number;
char grade [10];
} s[50];
int main() {
int x, i;
// x is the total no. of students
printf("Enter the number of students: ");
scanf("%d", &x);
// To 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 Grade: ");
scanf("%s", s[i].grade);
}
// To 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 Grade: ");
puts(s[i].grade);
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: Tony
Enter the last name: Stark
Enter the Grade: o O
Information for Roll Number: 2
Enter the first name: Steven
Enter the last name: Steve
Enter the Grade: A
Information for Roll Number: 3
Enter the first name: Chi ris
Enter the last name: Hems
Enter the Grade: B= +
Information for Roll Number: 4
Enter the first name: Loki
Enter the last name: Odin
Enter the Grade: O+
Information for Roll Number: 5
Enter the first name: Black
Enter the last name: Widow
Enter the Grade: A+
Display the student's information:
The Roll Number: 1
The First Name: Tony
The Last Name: Stark
The Grade: O
The Roll Number: 2
The First Name: Steven
The Last Name: Steve
The Grade: A
The Roll Number: 3
The First Name: Cris
The Last Name: Hems
The Grade: B+
The Roll Number: 4
The First Name: Loki
The Last Name: Odin
The Grade: O+
The Roll Number: 5
The First Name: Black
The Last Name: Widow
The Grade: A+