Practical / Solution
Unions — Exercise 1
Problem
Write a C program to create a union containing an integer, a float and
a character. Store and display one member at a time.
Program
#include <stdio.h>
union Data
{
int number;
float value;
char letter;
};
int main()
{
union Data d;
d.number = 25;
printf("Integer value = %d\n", d.number);
d.value = 12.50;
printf("Float value = %.2f\n", d.value);
d.letter = 'A';
printf("Character value = %c\n", d.letter);
return 0;
}
Explanation
A union allows different members to share the same memory location.
Therefore, only the member assigned most recently should be considered
as the currently stored value.
Expected Output
Integer value = 25
Float value = 12.50
Character value = A
Unions — Exercise 2
Problem
Write a C program to store and display an employee's ID using a union.
Program
#include <stdio.h>
union Employee
{
int id;
float salary;
char grade;
};
int main()
{
union Employee e;
e.id = 501;
printf("Employee ID = %d\n", e.id);
return 0;
}
Explanation
The union contains three possible members, but this program uses only
the id member at the current time.
Expected Output
Unions — Exercise 3
Problem
Write a C program to demonstrate that different members of a union
share the same memory location by displaying their addresses.
Program
#include <stdio.h>
union Data
{
int number;
float value;
char letter;
};
int main()
{
union Data d;
printf("Address of number = %p\n", (void *)&d.number);
printf("Address of value = %p\n", (void *)&d.value);
printf("Address of letter = %p\n", (void *)&d.letter);
return 0;
}
Explanation
All members of a union begin at the same memory location. Therefore,
their addresses are the same, although their data types are different.
Expected Output
Address of number = 0x7ffee...
Address of value = 0x7ffee...
Address of letter = 0x7ffee...
Exact memory addresses may differ from one system to another.
Unions — Exercise 4
Problem
Write a C program to demonstrate the difference between the memory
occupied by a structure and a union containing the same members.
Program
#include <stdio.h>
struct DataStructure
{
int number;
float value;
char letter;
};
union DataUnion
{
int number;
float value;
char letter;
};
int main()
{
struct DataStructure s;
union DataUnion u;
printf("Size of structure = %zu bytes\n",
sizeof(s));
printf("Size of union = %zu bytes\n",
sizeof(u));
return 0;
}
Explanation
Structure members normally have separate storage, while union members
share the same storage. Therefore, the total size of a union is based
mainly on its largest member, subject to alignment and padding rules.
Expected Output
Size of structure = implementation-dependent
Size of union = implementation-dependent
The exact values may vary depending on the compiler and system.
Unions — Exercise 5
Problem
Write a C program to take an integer, float or character as selected
by the user and store it in a union.
Program
#include <stdio.h>
union Data
{
int number;
float value;
char letter;
};
int main()
{
union Data d;
int choice;
printf("1. Integer\n");
printf("2. Float\n");
printf("3. Character\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter integer: ");
scanf("%d", &d.number);
printf("Stored integer = %d\n", d.number);
break;
case 2:
printf("Enter float: ");
scanf("%f", &d.value);
printf("Stored float = %.2f\n", d.value);
break;
case 3:
printf("Enter character: ");
scanf(" %c", &d.letter);
printf("Stored character = %c\n", d.letter);
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Explanation
A union is useful when only one of several possible data types needs to
be stored at a time. The selected member determines the currently valid
interpretation of the shared memory.
Expected Output
1. Integer
2. Float
3. Character
Enter your choice: 2
Enter float: 45.5
Stored float = 45.50
Unions — Exercise 6
Problem
Write a C program to store a product price and display it using a
union.
Program
#include <stdio.h>
union Product
{
int productId;
float price;
char code;
};
int main()
{
union Product p;
printf("Enter product price: ");
scanf("%f", &p.price);
printf("Product Price = %.2f\n", p.price);
return 0;
}
Explanation
The union contains different possible product-related values. In this
example, the price member is used to store a floating-point
value.
Expected Output
Enter product price: 1250.75
Product Price = 1250.75
Unions — Exercise 7
Problem
Write a C program to overwrite one union member with another and
observe the change in the stored value.
Program
#include <stdio.h>
union Data
{
int number;
char letter;
};
int main()
{
union Data d;
d.number = 65;
printf("Integer value = %d\n", d.number);
d.letter = 'A';
printf("Character value = %c\n", d.letter);
return 0;
}
Explanation
Since union members share the same memory, assigning a new member
overwrites the shared storage. After d.letter = 'A', the
character member is the value that should be used.
Expected Output
Integer value = 65
Character value = A
Unions — Exercise 8
Problem
Write a C program to use a union inside a program that stores a value
either as an integer or as a floating-point number according to the
user's choice.
Program
#include <stdio.h>
union Number
{
int integerValue;
float floatValue;
};
int main()
{
union Number n;
int choice;
printf("1. Integer\n");
printf("2. Float\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice == 1)
{
printf("Enter integer: ");
scanf("%d", &n.integerValue);
printf("Value = %d\n", n.integerValue);
}
else if (choice == 2)
{
printf("Enter float: ");
scanf("%f", &n.floatValue);
printf("Value = %.2f\n", n.floatValue);
}
else
{
printf("Invalid choice.\n");
}
return 0;
}
Explanation
The union provides one shared storage area for either an integer or a
floating-point value. The program chooses which member to use at runtime.
Expected Output
1. Integer
2. Float
Enter choice: 1
Enter integer: 250
Value = 250
Unions — Exercise 9
Problem
Write a C program to create a union containing student roll number,
marks and grade, and demonstrate storing each member one after another.
Program
#include <stdio.h>
union StudentData
{
int roll;
float marks;
char grade;
};
int main()
{
union StudentData s;
s.roll = 101;
printf("Roll Number = %d\n", s.roll);
s.marks = 88.5;
printf("Marks = %.2f\n", s.marks);
s.grade = 'A';
printf("Grade = %c\n", s.grade);
return 0;
}
Explanation
Only one member is treated as the active stored value at a time.
Assigning another member uses the same shared memory and replaces
the previous representation.
Expected Output
Roll Number = 101
Marks = 88.50
Grade = A
Unions — Exercise 10
Problem
Write a C program to demonstrate a practical use of a union for
storing different types of sensor readings where only one reading
type is required at a time.
Program
#include <stdio.h>
union SensorReading
{
int temperature;
float voltage;
float pressure;
};
int main()
{
union SensorReading sensor;
int choice;
printf("1. Temperature\n");
printf("2. Voltage\n");
printf("3. Pressure\n");
printf("Enter reading type: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter temperature: ");
scanf("%d", &sensor.temperature);
printf("Temperature = %d C\n",
sensor.temperature);
break;
case 2:
printf("Enter voltage: ");
scanf("%f", &sensor.voltage);
printf("Voltage = %.2f V\n",
sensor.voltage);
break;
case 3:
printf("Enter pressure: ");
scanf("%f", &sensor.pressure);
printf("Pressure = %.2f\n",
sensor.pressure);
break;
default:
printf("Invalid reading type.\n");
}
return 0;
}
Explanation
A union can be useful when an application needs to store one of several
alternative data types but does not need all of them simultaneously.
This can reduce memory usage compared with storing all alternatives
separately.
Expected Output
1. Temperature
2. Voltage
3. Pressure
Enter reading type: 2
Enter voltage: 230.5
Voltage = 230.50 V