C program to read roll no and name of students by using array of structure
Introduction:
The C programming language is widely used in software development and programming. It offers powerful features, flexibility, and control to create efficient programs. One of the most common programming tasks is to read and store information from users. In this article, we'll dive deep into creating a C program that reads roll numbers and names of students using an array of structures. This program is a must-know for aspiring developers, and mastering it can significantly enhance your coding skills.
What is an Array of Structure?
Before we dive into the program, let's understand what an array of structure is. A structure in C is a user-defined data type that allows you to combine different data types under a single name. An array is a collection of similar data types that are stored in contiguous memory locations. An array of structure, as the name suggests, is an array where each element is of structure type. It means that each element of the array can store different data types, making it a versatile and efficient way to store and manage data.
Writing program
#include<stdio.h>
#include<conio.h>
main()
{
struct student
{
int roll;
char name[20];
};
struct student stu[5]; /* array of structure */
int i;
for(i=0;i<=4;i++)
{
printf("\nEnter rollno and name of student ");
scanf("%d%s",&stu[i].roll,stu[i].name);
}
for(i=0;i<=4;i++)
printf("\n%d %s",stu[i].roll,stu[i].name);
getch();
}
Explanation of the program
The purpose of this program is to read the roll numbers and names of five students and store them in an array of structures. It then displays the roll numbers and names of all five students on the screen.
The program starts by including two header files - "stdio.h" and "conio.h". The "stdio.h" file is used to provide standard input/output functions, while "conio.h" is used to include the "getch()" function that waits for a character to be entered before closing the program.
The next step is to define the structure "student" that contains two fields - "roll" and "name". The "roll" field is of type integer, and the "name" field is of type character array with a size of 20.
After defining the structure, an array of structures "stu" is created with a size of 5, which means it can hold information for five students.
Then, a loop is used to prompt the user to enter the roll number and name for each of the five students. The "scanf()" function is used to read the input and store it in the corresponding structure elements.
Finally, another loop is used to display the roll numbers and names of all five students on the screen using the "printf()" function.
At the end of the program, the "getch()" function is used to wait for a character to be entered before closing the program.
Overall, this program demonstrates how to use structures and arrays in C to store and manage multiple students' information efficiently.
Output:
FAQs
Q. What is the purpose of using an array of structures in this program?
Ans: An array of structures allows us to store multiple students' information in a single data structure. It makes the program more efficient and versatile.
Q. Can I modify the program to include additional information, such as the student's address or contact number?
Ans: Yes, you can modify the program to include additional information by adding more fields to the structure and modifying the input and output statements accordingly.
Q. What are the benefits of mastering this program?
Ans: Mastering this program can significantly enhance your coding skills and prepare you for more complex programming tasks. It also helps you understand the basics of structures, arrays, loops, and input/output statements in C.
Conclusion
In conclusion, creating a C program to read roll numbers and names of students by using an array of structures is a crucial programming task. It allows you to store and manage multiple students' information efficiently and prepares you for more advanced programming tasks. This article offered step-by-step instructions, tips, and tricks to help you master the program and elevate your coding skills. Practice regularly, use meaningful variable names, and follow proper formatting and indentation to make your code more readable and understandable. With dedication and practice, you can become a proficient C programmer and tackle complex programming challenges with ease.
No comments:
Post a Comment