Unions aren't always scary!

Unions in C are user-defined data types that allow different data members to share the same memory space. They are particularly useful when dealing with situations where different data members need to occupy the same memory space at different times, effectively conserving memory resources.

Here's a simple situation where a union can be used: Let's say you are working on a system that needs to store the temperature of a location, which can be represented in either Celsius or Fahrenheit. Instead of using separate variables for Celsius and Fahrenheit, you can use a union to store the temperature in a single memory location, switching between Celsius and Fahrenheit as needed.

Here's a sample code with comments to illustrate this situation:

#include <stdio.h>

// Define a union to store temperature in Celsius and Fahrenheit
union Temperature {
    float celsius;
    float fahrenheit;
};

int main() {
    union Temperature temp;

    // Store temperature in Celsius
    temp.celsius = 28.5;
    printf("Temperature in Celsius: %.2f\n", temp.celsius);

    // Switch to Fahrenheit and store temperature
    temp.fahrenheit = (temp.celsius * 9 / 5) + 32;
    printf("Temperature in Fahrenheit: %.2f\n", temp.fahrenheit);

    return 0;
}

In this example, the union Temperature allows us to store the temperature in either Celsius or Fahrenheit using the same memory location. We can easily switch between the two representations based on our requirements.

1
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.