Advertisement
/* An example of array of structure */
#include<stdio.h>
int main()
{
struct bag
{
char gender_type;
int year_of_make;
float price;
};
struct bag b1[2];
for(int i=0;i<2;i++)
{
printf("Enter the details of %d bag",(i+1));
printf("\n");
printf("Enter the year of make of the bag :");
scanf("%d", &b1[i].year_of_make);
getchar(); /* using getchar() function is important here, as it consumes the enter pressed after entering an int value, */
/* which doesn't bother next scanf() function from taking a char value */
printf("Enter the gender type of the bag :");
scanf("%c", &b1[i].gender_type);
printf("Enter the price of the bag :");
scanf("%f", &b1[i].price);
}
for(int i=0;i<2;i++)
{
printf("The gender type of the bag : %c", b1[i].gender_type);
printf("\n");
printf("The year of make of the bag : %d", b1[i].year_of_make);
printf("\n");
printf("The price of the bag : : %.2f", b1[i].price);
printf("\n");
}
return 0;
}
Enter the details of 1 bag
Enter the year of make of the bag :2017
Enter the gender type of the bag :M
Enter the price of the bag :9.8
Enter the details of 2 bag
Enter the year of make of the bag :2018
Enter the gender type of the bag :F
Enter the price of the bag :10.9
The details of 1 bag
The gender type of the bag : M
The year of make of the bag : 2017
The price of the bag : : 9.80
The details of 2 bag
The gender type of the bag : F
The year of make of the bag : 2018
The price of the bag : : 10.90
struct bag
{
char gender_type;
int year_of_make;
float price;
};
Advertisement
struct bag b1[2];
printf("Enter the year of make of the bag :");
scanf("%d", &b1[i].year_of_make);
printf("The gender type of the bag : %c", b1[i].gender_type);
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement