Structure:
- Structure is the derived data type.
- Structure is the collection of multiple data elements that can be of different data types.
Syntax:
Struct structure_name{
data_type variable_name;
data_type variable_name;
-
-
}
Example:
struct employee {
int code;
struct {
char fn[20];
char ln[20];
}
name;
float sal;
struct {
int date, month, year;
}
DOJ;
};
Program:
#include<stdio.h>
#include<conio.h>
struct employee{
int code;
struct{
char fn[20];
char ln[20];
}
name;
float sal;
struct{
int date, month, year;
}
DOJ;
};
int main(){
struct employee e[100];
int i, n;
printf("Enter number of employees:");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("\nEnter Code:");
scanf("%d", &e[i].code);
printf("\nEnter First Name:");
gets(e[i].name.fn);
printf("\nEnter Last Name:");
gets(e[i].name.ln);
printf("\nEnter Salary:");
scanf("%d", &e[i].sal);
printf("\nEnter Date of Joining\n");
scanf("%d", &e[i].DOJ.date);
printf("\nEnter Month of Joining\n");
scanf("%d", &e[i].DOJ.month);
printf("\nEnter Year of Joining\n");
scanf("%d", &e[i].DOJ.year);
}
for(i=0; i<n; i++) {
printf("Code:%d\n", e[i].code);
printf("First Name:%s\n", e[i].name.fn);
printf("Last Name:%s\n", e[i].name.ln);
printf("Salary:%d\n", e[i].sal);
printf("Date of Joining:%d\n", e[i].DOJ.date);
printf("Month of Joining:%d\n", e[i].DOJ.month);
printf("Year of Joining:%d\n", e[i].DOJ.year);
}
getch();
}
Output:
Enter number of employees: 10
Enter Code: 01
Enter First Name: PHIL
Enter Last Name: JONES
Enter Salary: 50000
Enter Date of Joining: 01
Enter Month of Joining: 03
Enter Year of Joining: 2014
Enter Code: 02
Enter First Name: WAYNE
Enter Last Name: ROONEY
Enter Salary: 40000
Enter Date of Joining: 12
Enter Month of Joining: 07
Enter Year of Joining: 2014
Enter Code: 10
Enter First Name: GARY
Enter Last Name: CAHIL
Enter Salary: 40000
Enter Date of Joining: 15
Enter Month of Joining: 08
Enter Year of Joining: 2013