PIC Exp.7
PIC Exp.7
Date of
DIV/ Batch No: P2-3
Performance:
Experiment No: 7
Title: Structures and unions
COs to be achieved:
CO4: Design modular programs using functions and the use of structure and union.
Theory:
Introduction to Structures
A structure is a user-defined data type in C that groups variables of different types under a single
name. Structures are used when you need to store multiple related pieces of data, such as
information about a student, employee, or product, where each field might have a different data
type (e.g., integers, floats, and characters).
Example: A structure could be defined to store a student's name, age, and grade.
To declare and define a structure in C, you first use the struct keyword, followed by a structure
name, and the members enclosed within curly braces {}. Each member can be of a different data
type.
Syntax:
struct structure_name {
data_type member1;
data_type member2;
// more members
};
Example:
struct Student {
char name[50];
int age;
float grade;
};
This defines a structure Student with three members: a string for the name, an integer for the age,
and a float for the grade.
Structure Initialization
Structures can be initialized at the time of declaration or later by assigning values to their members
individually. If initialization during declaration, values for the members are assigned in the same
order as their declaration.
Example:
student1.age = 21;
strcpy(student1.name, "Alice");
student1.grade = 90.0;
Structure members can be accessed using the dot (.) operator. The member values can be printed or
manipulated as required.
Syntax:
variable_name.member_name
Example:
If a structure is pointed to by a pointer, the arrow (->) operator is used to access members.
Example:
Array of Structures
An array of structures is used when you want to store multiple instances of a structure. Each
element of the array is a structure.
Example:
students[0].age = 20;
strcpy(students[0].name, "Alice");
students[0].grade = 90.0;
Introduction to Unions
A union is a user-defined data type similar to a structure, but with one key difference: all members
of a union share the same memory location. This means that at any given time, only one member of
the union can hold a value, making it more memory efficient when you don't need to store multiple
values simultaneously.
Syntax:
union union_name {
data_type member1;
data_type member2;
// more members
};
Example:
union Data {
int i;
float f;
char str[20];
};
In the above example, the Data union can store an integer, a float, or a string, but only one of these
at a time. The memory allocated for all the members of the union is the size of the largest member.
Just like structures, union members are accessed using the dot (.) operator. However, because all
members share the same memory space, modifying one member will overwrite the other members'
values.
Example:
Problem Statements:
Design a C program to manage employee data using structures and unions. The program should
allow the following functionalities:
▪ Employee ID (integer)
▪ Name (string)
▪ Age (integer)
▪ Department (string)
▪ Commission (float)
o For each employee, calculate the total salary based on their role:
o Allow the user to search for an employee by their Employee ID and display their
details.
o Allow the user to update specific details of an employee (e.g., name, age,
department, or role-specific data).
Requirements:
2. Use a union to store role-specific attributes (either for sales or technical employees).
5. Provide a menu-driven interface for the user to perform the above operations.
Code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee {
int id;
char name[50];
int age;
char department[30];
float basic_salary;
};
void addEmployee() {
scanf("%d", &employees[num_employees].id);
scanf("%d", &employees[num_employees].age);
scanf("%f", &employees[num_employees].basic_salary);
if (strcmp(employees[num_employees].role, "Sales") == 0) {
} else {
scanf("%f", &employees[num_employees].extra);
num_employees++;
void displayEmployees() {
if (num_employees == 0) {
return;
printf("\nEmployee List:\n");
if (strcmp(employees[i].role, "Sales") == 0) {
} else {
void calculateTotalSalary() {
float total = 0;
void searchEmployee() {
int search_id;
scanf("%d", &search_id);
if (employees[i].id == search_id) {
printf("\nEmployee Found:\n");
return;
void updateEmployee() {
int update_id;
scanf("%d", &update_id);
if (employees[i].id == update_id) {
scanf("%d", &employees[i].age);
scanf("%f", &employees[i].basic_salary);
return;
void deleteEmployee() {
int delete_id;
scanf("%d", &delete_id);
if (employees[i].id == delete_id) {
num_employees--;
return;
void showMenu() {
int choice;
do {
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
// Main function
int main() {
showMenu();
return 0;
Output:
ANSWER:
1. Structure: Allocates separate memory for each member, allowing simultaneous access.
2. Union : Allocates shared memory, so only one member holds valid data at a time.
Conclusion:
This experiment explored structures and unions in C for efficient data management. Structures
stored multiple attributes, while unions optimized memory. We implemented key functionalities,
reinforcing concepts like data abstraction, memory management, and modular programming.