C Programming Tutorial

C Programming Tutorial

Union in C Programming

Hello Everyone, In this article, we will discuss Union in C Programming. Unions are a special data type that allows you to store different data types in the same memory location. By learning about unions, you will better understand how to save memory and manage different data structures efficiently. Whether you are new to C or have some experience, this guide will help you to understand basics of Array of Structures in C effectively with examples. Let’s get started.

A union in C programming is a user-defined data type that allows multiple members (variables) to share the same memory location. This means that only one member can store a value at any given time. Unions are similar to structures in C, but they provide a more efficient way of using memory when you need to store only one value from a collection of different data types.

Here’s an example to demonstrate the use of unions in C:

#include <stdio.h>// Declare a union called Data
union Data {
   int i;
   float f;
   char str[20];
};

int main() {
   union Data data; // Declare a variable of type Data

   // Store an integer value
   data.i = 10;
   printf("data.i: %d\\n", data.i);

   // Store a float value (this will overwrite the previous value)
   data.f = 3.14;
   printf("data.f: %.2f\\n", data.f);

   // Store a string value (this will overwrite the previous value)
   strcpy(data.str, "Hello World");
   printf("data.str: %s\\n", data.str);

   return 0;
}

In this example, we define a union called Data with three members: an integer (i), a float (f), and a string (str). In the main function, we declare a variable data of type Data and then store an integer, a float, and a string value in it.

The output of this program would be:

data.i: 1078523331
data.f: 3.14
data.str: Hello World

Notice that when we print the value of data.i, it doesn’t display the value 10 that we assigned earlier. This is because the memory location is shared between all the members of the union, and the float value 3.14 overwrote the integer value 10. Similarly, the string value “Hello World” overwrote the previous float value, but since we don’t try to print the float value again, this doesn’t cause any confusion.

How to Define and Access a Union

Defining and accessing a union in C programming is quite straightforward. Here’s a step-by-step explanation:

1. Define a union: To define a union, you use the union keyword, followed by the name of the union, and then a pair of curly braces {} containing the union’s members (variables). Each member should have a specific data type.

    union ExampleUnion {
       int intValue;
       float floatValue;
       char string[20];
    };
    

    In this example, we defined a union called ExampleUnion with three members: an integer (intValue), a float (floatValue), and a string (string).

    2. Declare a variable of the union type: To declare a variable of the union type, you use the union’s name followed by the variable name. For instance:

      union ExampleUnion myData;

      This declares a variable called myData of type ExampleUnion.

      3. Access the union members: To access the members of a union variable, you use the dot operator (.), similar to how you access members of a structure. For example:

        myData.intValue = 42;
        myData.floatValue = 3.14;
        strcpy(myData.string, "Hello, Union!");
        

        Here’s a complete example:

        #include <stdio.h>
        #include <string.h>// Define a union
        union ExampleUnion {
           int intValue;
           float floatValue;
           char string[20];
        };
        
        int main() {
           // Declare a variable of the union type
           union ExampleUnion myData;
        
           // Access and set union members
           myData.intValue = 42;
           printf("myData.intValue: %d\\n", myData.intValue);
        
           myData.floatValue = 3.14;
           printf("myData.floatValue: %.2f\\n", myData.floatValue);
        
           strcpy(myData.string, "Hello, Union!");
           printf("myData.string: %s\\n", myData.string);
        
           return 0;
        }
        

        Output:

        myData.intValue: 1078523331
        myData.floatValue: 3.14
        myData.string: Hello, Union!
        

        Remember that union members share the same memory location, so when you set a value for one member, it might overwrite the values of other members.

        Share This Article