Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,948 questions

51,890 answers

573 users

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 217 views
1 answer 99 views
1 answer 117 views
117 views asked Apr 27, 2017 by avibootz
2 answers 234 views
2 answers 157 views
...