Today, in this article we are going to discuss the Use of Arrays in C Structures. As in the previous article, we discussed the start of Structures, a C Program to Print Bills using Structures. But in this article, we will discuss the Arrays used in C structures.
Use Arrays in C Structures
As arrays are used to store homogenous data in contiguous locations. we use arrays in structures and will tell you how to use Arrays in C Structures. We will show how we are going to use arrays in a C structure.
Program
Write a C program in which Arrays are used in Structures.
#include<stdio.h> struct student { int roll_no; char name[20]; float marks; }; int main() { int i; struct student s[3]; printf("enter roll no,name and marks\n"); for(i=0;i<3;i++) { scanf("%d\n%s\n%f",&s[i].roll_no,&s[i].name,&s[i].marks); } for(i=0;i<3;i++) { printf("\nroll no =%d\nname=%s\nmarks=%f",s[i].roll_no,s[i].name,s[i].marks); } }
Output
In this article, we are going to share information about using arrays in structures in C. First of all, we will declare the data type of the structure which is a struct, and with that a tag and also a variable which is optional.
Then, under the struct, we will declare the variables that we will use in our program which are roll number, name, and marks of a student. in this article, we give information about three students using arrays and for loops.
Then, we will start our main body, in which we will declare arrays that are of size three. then we will use for loop which will run three times in the program. the loop will show and ask the user to input data which is known as arrays.
And after all, in the last step, we will output the following data using for loop and that for loop will give us the role number of three students, the name of three students, and also the marks of that three students.
Conclusion
The above article is showing us how we are using arrays in this program which is structured in C program in a very easy and simple way. this program will tell us the information on the whole usage of arrays in C structure programs.