Theory of Structure and Union
Definition of Structure : The user defined data type which can
store the data of various data types such as int, char etc. is called Structure.
It is used to hold all the facts of single objects.
Example:Book [publication, title, author, price,page];
Student [roll no, class, name, telephone no, age];
There are 3 phases in structure.
1. Design Phase
2. Declaration phase
3. Data access phase
1. Design Phase: It specifies
a) Name of user defined data type
b) Number of member data of specified data type
c) Data type of each number variable
Example of design phase
struct book
{
int page, book no;
char title[20],author[20],publication[20];
float price;
};
2. Declaration phase: It declares a variable of user defined data type.
Example of declaration phase:
struct book
{
int page, book no;
char title[20],author[20],publication[20];
float price;
};
struct book comp;
3. Data Access: To access member variable of structure data type we use dot operator.
Example of data access:
comp.page, comp.pageno, comp.title, comp.author,comp.publication etc
Size of structure:
The size of the structure variable is directly depends on its member variable size. In the following example, the memory size of the structure variable S is 64.
Syntax:
sizeof(structure _name);
C program to show the size of structure
#include<stdio.h>
struct student
{
int roll_no; // 4 Bytes
char fname[30]; //30*1=30 Bytes
char lname[30]; // 30 Bytes
} S;
main()
{
printf("%d",sizeof(S));
}
Definition of Union:
A collection of heterogeneous data type is called Union. The amount of memory required to store a union is equal to the size required by the largest member of the union. Only one member can be accessed at a time because only one member of the union is active at a time.
union keyword is used to define union.
Syntax:
union union_name
{
datatype memebr1 ;
datatype memebr2 ;
.................
datatype member n ;
} variable;
Example of union:
union data
{
char x;
int y;
float z;
} var;
Memory size for var is 4 bytes.
1. Write a program considering user defined variable distance with numbers: feet and inch. Calculate the sum of two different distances.
C program to calculate sum of two different distance using structure
#include<stdio.h>
struct distance
{
int inch;
int feet;
}d1,d2,total;
main()
{
printf("Enter first feet and inch \n");
scanf("%d%d",&d1.feet,&d1.inch);
printf("Enter second feet and inch \n");
scanf("%d%d",&d2.feet,&d2.inch);
total.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;
total.inch=(d1.inch+d2.inch)%12;
printf("Total Feet=%d Inch=%d\n",total.feet,total.inch);
}Output
Enter first feet and inch
6
8
Enter second feet and inch
5
6
Total Feet=12 Inch=2
2. Write a program considering user defined variable time with members: hh , mm and ss. Calculate the sum of two different times.
C program to calculate sum of two different times using structure
#include<stdio.h>
struct sum
{
int hh;
int mm;
int ss;
}t1,t2,total;
main()
{
int totalmm;
printf("Enter time-1 in hour , minutes and seconds \n");
scanf("%d%d%d",&t1.hh,&t1.mm,&t1.ss);
printf("Enter time-2 in hour, minutes and seconds\n");
scanf("%d%d%d",&t2.hh,&t2.mm,&t2.ss);
totalmm=t1.mm+t2.mm+(t1.ss+t2.ss)/60;
total.ss=(t1.ss+t2.ss)%60;
total.mm=totalmm%60;
total.hh=t1.hh+t2.hh+totalmm/60;
printf("Total Hours=%d Minutes=%d and Seconds=%d\n",total.hh,total.mm,total.ss);
}Output
Enter time-1 in hour , minutes and seconds
2
30
45
Enter time-2 in hour, minutes and seconds
1
32
20
Total Hours=4 Minutes=3 and Seconds=5
3. Consider structure student with members: sid, name and height. Write a program that takes maximum 100 records, rearrange the records in ascending order on the basis of height and prints them.
C program to rearranging records in ascending order on the basis of height using structure
#include<stdio.h>
struct student
{
int sid;
char name[30];
float height;
}s[100],temp;
main()
{
int i,j,n;
for(i=0;i<100;i++)
{
printf("Enter Student id , Name and Height \n");
scanf("%d%s%f",&s[i].sid,s[i].name,&s[i].height);
}
for(i=0;i<100;i++)
{
for(j=i+1;j<100;j++)
{
if(s[i].height>s[j].height)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
printf("Records in ascending order on the basis of height \n");
printf("Student ID\t Name\t Height \n");
for(i=0;i<100;i++)
printf("%d\t %s\t %f \n",s[i].sid,s[i].name,s[i].height);
}
}Output
For testing purpose taken three records only
Enter Student id , Name and Height
1
alex
5.9
Enter Student id , Name and Height
2
anjali
5.6
Enter Student id , Name and Height
3
jeni
5.5
Records in ascending order on the basis of height
Student ID Name Height
3 jeni 5.500000
2 anjali 5.600000
1 alex 5.900000
4. Write a program that takes empid, name and salary of 100 employees and search a particular employee's record in the array of structure on the basis of empid. Also find the position of the record.
C program that takes empid, name and salary of 100 employees and search a particular employee's record in the array of structure on the basis of empid. Also find the position of the record
#include<stdio.h>
struct employee
{
int empid;
char ename[30];
float esalary;
} e[100];
main()
{
int i,flag=0;
int searchid;
for(i=0;i<100;i++)
{
e[i].empid=i+1;
printf("Employee No %d Name and Salary ",i+1);
scanf("%s%f",e[i].ename,&e[i].esalary);
}
printf("enter emp id to be searched \n");
scanf("%d",&searchid);
for(i=0;i<100;i++)
{
if(e[i].empid==searchid)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("Employee ID No is %d. Employee Name is %s and Salary is %0.2f \n",e[i].empid,e[i].ename,e[i].esalary);
printf("Also Position of record is %d\n ",i+1);
}
else
{
printf("Record not found\n");
}
}Output
For testing purpose taken three records only
Employee No 1 Name and Salary alex
50000
Employee No 2 Name and Salary anjali
45000
Employee No 3 Name and Salary jeni
48000
enter emp id to be searched
3
Employee ID No is 3. Employee Name is jeni and Salary is 48000.00
Also Position of record is 3
5. Define structure type employee with members: empid, name , post and bsalary. Write a C program to print the record of the employee who has maximum salary.
C program to print the record of the employee who has maximum salary using structure
// Suppose there are three employee's records
#include<stdio.h>
struct employee
{
int empid;
char name[30];
char post[30];
float bsalary;
}e[10];
main()
{
float max;
int i,p=0;
for(i=0;i<3;i++)
{
e[i].empid=i+1;
printf("Employee No %d Name , Post and Basic Salary ",i+1);
scanf("%s%s%f",e[i].name,e[i].post,&e[i].bsalary);
}
max=e[0].bsalary;
for(i=0;i<3;i++)
{
if(e[i].bsalary>max)
{
max=e[i].bsalary;
p=i;
}
}
printf("Employee ID No %d - %s has maximum salary %0.2f \n",e[p].empid,e[p].name,e[p].bsalary);
}Output
Employee No 1 Name , Post and Basic Salary alex
doctor
75000
Employee No 2 Name , Post and Basic Salary jeni
manager
80000
Employee No 3 Name , Post and Basic Salary anjali
accountant
50000
Employee ID No 2 - jeni has maximum salary 80000.00