Pages

This blog is under construction

Monday, November 18, 2019

Array of Structure in C

There is a structure called employee that holds information like employee code, name, date of joining. Write a program to create an array of the structure and enter some data into it.


#include<stdio.h>

struct date{
int day, month, year;
};

struct employee
{
int code;
char emp_name[20];
struct date doj;
};


void main()
{
int n,i;

printf("Enter the total number of employees\n");
scanf("%d",&n);
struct employee emp[n];

for(i=0; i<n; i++)
{
printf("Enter the name\n");
scanf("%s",emp[i].emp_name);
printf("Enter the code");
scanf("%d",&emp[i].code);
printf("Enter the date\n");
scanf("%d%d%d",&emp[i].doj.day,&emp[i].doj.month,&emp[i].doj.year);

}

printf("Name\t code\tDay\tMonth\tYear\n");

for(i=0; i<n; i++)
{

printf("%s\t",emp[i].emp_name);

printf("%d\t",emp[i].code);
printf("%d\t%d\t%d\n",emp[i].doj.day,emp[i].doj.month,emp[i].doj.year);

}
}


Enter the total number of employees                                                                                                                    
2                                                                                                                                                      
Enter the name                                                                                                                                         
Amit                                                                                                                                                   
Enter the code101                                                                                                                                      
Enter the date                                                                                                                                         
11 12 2000                                                                                                                                             
Enter the name                                                                                                                                         
Joy                                                                                                                                                    
Enter the code102                                                                                                                                      
Enter the date                                                                                                                                         
10 10 1998                                                                                                                                             
Name     code   Day     Month   Year                                                                                                                   
Amit    101     11      12      2000                                                                                                                   
Joy     102     10      10      1998  

1 comment: