How to use simple struct array in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h> 
 
struct factory_workers
{
  int id;
  int age;
  float salary;
  char city[30];
};
 
int main(void)
{
    struct factory_workers worker[2];  // worker[2] is array of 2 factory_workers
    int i;
    
    for (i = 0; i < 2; i++)
    {
        printf("Enter ID: ");
        scanf("%d", &worker[i].id);
        printf("Enter Age: ");
        scanf("%d", &worker[i].age);
        printf("Enter Salary: ");
        scanf("%f", &worker[i].salary);
        printf("Enter City: ");
        scanf("%s", worker[i].city);
    }
    for (i = 0; i < 2; i++)
    {
        printf("id = %d\n", worker[i].id);
        printf("age = %d\n", worker[i].age);
        printf("salary = %.2f\n", worker[i].salary);
        printf("city = %s\n", worker[i].city);
    }
 
    return 0;
}
 
 

/*
run:
 
Enter ID: 12345
Enter Age: 48
Enter Salary: 3457
Enter City: NY
Enter ID: 67890
Enter Age: 46
Enter Salary: 4532
Enter City: LA
id = 12345
age = 48
salary = 3457.00
city = NY
id = 67890
age = 46
salary = 4532.00
city = LA

*/


answered Oct 7, 2014 by avibootz

Related questions

3 answers 238 views
1 answer 131 views
1 answer 124 views
124 views asked Apr 27, 2017 by avibootz
2 answers 251 views
2 answers 196 views
...