written 7.9 years ago by |
Arrays allow to define type of variables that can hold several data items of the same kind. Similarly, structure is another user-defined data type available in C that allows combining data items of different kinds. Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book:
- Title
- Author
- Subject
- Book ID
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows:
struct [structure tag]
{
member definition; member
definition; ...
member definition;
} [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. Here is the way you would declare the Book structure:
struct book
{
char title[50];
char author[50];
int pages;
int cost;
};
Nested Structure
Nested structure in C is nothing but structure within structure. One structure can be declared inside other structure as we declare structure members inside a structure.
Example:
struct date
{
int day;
int month;
int year
};
struct employee
{
char name[20];
char dept[20];
int salary;
struct date doj;
};
Program:
#include<stdio.h>
struct date
{
int day;
int month;
int year
};
struct employee
{
char name[20];
char dept[20];
int salary;
struct date doj;
};
main()
{
int i,n,
struct employee emp;
printf(“Enter number of employes:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\nEnter Employee Name:”);
scanf(“%s”,&emp.name[i]);
printf(“\nEnter Employee’s department:”);
scanf(“%s”,&emp.dept[i]);
printf(“\nEnter Employee Salary:”);
scanf(“%d”,&emp.salary[i]);
printf(“\nJoining Date:”);
printf(“\nEnter day:”);
scanf(“%d”,&emp.doj.day[i]);
printf(“\nEnter Month:”);
scanf(“%d”,&emp.doj.month[i]);
printf(“\nEnter Year:”);
scanf(“%d”,&emp.doj.year[i]);
}
printf(“\nDetails of Employees:”);
printf(“\nName\tDepartment\tSalary\tDate of Joining”);
printf(“\n............................................................................................................................”);
for(i=1;i<=n;i++)
{
printf(“\n%s\t%s\t%d\t%d/%d/%d”,emp.name[i],emp.dept[i],emp.salary[i],emp.doj.day[i],emp.doj.month[i],emp.doj.year[i]);
}
}
Output:
Enter Number of employees: 2
Enter Employee Name: Amar
Enter Employeeis department: Production
Enter Employee Salary: 30000
Joining Date:
Enter day: 12
Enter Month: 4
Enter Year: 2013
Enter Employee Name: Nikhil
Enter Employeeis department: Planning
Enter Employee Salary: 28000
Joining Date:
Enter day: 23
Enter Month: 8
Enter Year: 2015
Detais of Employees:
Name Department Salary Date of Joining
..............................................................................................
Amar Production 30000 12/4/2013
Nikhil Planning 28000 23/8/2015