This code is an example of using structures in C to manage data about Students.
#include <stdio.h>
struct student
{
int s_id;
char first_name[50];
char last_name[50];
char grade [5];
} students[10];
int main()
{
int total, x; // x to be used in a for loop
printf("Total Number of Students to Enter: ");
scanf("%d", &total);
printf("Enter Data: \n");
for (x = 0; x < total; x++)
{
printf("--------------------------------------------- \n");
students[x].s_id = x + 1;
printf("ID: %d \n", students[x].s_id);
printf("First name: ");
scanf("%s", students[x].first_name);
printf("Last name: ");
scanf("%s", students[x].last_name);
printf("Grade: ");
scanf("%s", students[x].grade);
}
printf("Print Data: \n");
for (x = 0; x < total; x++)
{
printf("--------------------------------------------- \n");
printf("ID: %d \n", x + 1);
printf("First Name: ");
puts(students[x].first_name);
printf("Last Name: ");
puts(students[x].last_name);
printf("Grade: ");
puts(students[x].grade);
printf("\n");
}
return 0;
}
Result:
Total Number of Students to Enter: 2
Enter Data:
---------------------------------------------
ID: 1
First name: Michael
Last name: Smith
Grade: 10
---------------------------------------------
ID: 2
First name: Samantha
Last name: Fox
Grade: 10
Print Data:
---------------------------------------------
ID: 1
First Name: Michael
Last Name: Smith
Grade: 10
---------------------------------------------
ID: 2
First Name: Samantha
Last Name: Fox
Grade: 10
Process returned 0 (0x0) execution time : 27.957 s
Press any key to continue.
Here's a line-by-line explanation:
#include <stdio.h>
- This is a preprocessor directive that includes the standard input-output library header file in the program.
struct student
{
int s_id;
char first_name[50];
char last_name[50];
char grade [5];
} students[10];
This block of code defines a structure called student
with four members: s_id
, first_name
, last_name
, and grade
. An array of 10 student
structures is also defined.
int main()
{
int total, x;
This block declares two integer variables: total
and x
.
printf("Total Number of Students to Enter: ");
scanf("%d", &total);
This block prints a message asking the user to enter the total number of students they want to input, then reads the input from the user and stores it in the variable total
.
printf("Enter Data: \n");
for (x = 0; x < total; x++)
{
printf("--------------------------------------------- \n");
students[x].s_id = x + 1;
printf("ID: %d \n", students[x].s_id);
printf("First name: ");
scanf("%s", students[x].first_name);
printf("Last name: ");
scanf("%s", students[x].last_name);
printf("Grade: ");
scanf("%s", students[x].grade);
}
This block uses a for loop to iterate through the array of students
and prompts the user to enter the data for each student. The student ID is automatically assigned to the index of the student in the array, starting from 1.
printf("Print Data: \n");
for (x = 0; x < total; x++)
{
printf("--------------------------------------------- \n");
printf("ID: %d \n", x + 1);
printf("First Name: ");
puts(students[x].first_name);
printf("Last Name: ");
puts(students[x].last_name);
printf("Grade: ");
puts(students[x].grade);
printf("\n");
}
This block prints out the data entered for each student in a user-friendly format using a for loop to iterate through the array of students
.
return 0;
}
This line signals to the operating system that the program has run successfully and the program ends.
No comments:
Post a Comment