1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

Learn Unions in C Programming with Codes, Examples, and Explanations in 2023

Article by: Manish Methani

Last Updated: November 1, 2021 at 2:04pm IST
3 min 38 sec read

Unions are a type of data structure in C programming that allow you to store different types of data in the same memory space. In this tutorial, we will cover what unions are, how to create them, and how to use them in your code with practical examples and explanations.

Creating a Union:

To create a union in C programming, you use the same syntax as creating a struct, but use the keyword union instead of struct. Here is an example of how to create a union:

#include <stdio.h>

union data {
    int i;
    float f;
    char str[20];
};

int main() {
    union data d1;
    d1.i = 10;
    printf("d1.i = %d", d1.i);
    d1.f = 3.14;
    printf("d1.f = %.2f", d1.f);
    strcpy(d1.str, "Hello");
    printf("d1.str = %s", d1.str);
    return 0;
}

In the above example, we define a union named data with three members: an integer, a float, and a character array. We then create a variable d1 of type union data and assign a value to its integer member. We then print the value of d1.i. Next, we assign a value to the float member of d1 and print it. Finally, we assign a string to the character array member of d1 and print it.

Using a Union:

Unions are useful when you want to store different types of data in the same memory space. Here is an example of how to use a union to store a value of different types:

#include <stdio.h>

union data {
    int i;
    float f;
};

void print_data(union data d, int type) {
    if (type == 0) {
        printf("Data as integer: %d", d.i);
    } else if (type == 1) {
        printf("Data as float: %.2f", d.f);
    }
}

int main() {
    union data d1;
    d1.i = 10;
    print_data(d1, 0); // pass union as integer
    d1.f = 3.14;
    print_data(d1, 1); // pass union as float
    return 0;
}

In the above example, we define a union named data with two members: an integer and a float. We then define a function named print_data that takes a union data variable and a type parameter. If the type parameter is 0, the function prints the value of the integer member. If the type parameter is 1, the function prints the value of the float member. We then create a variable d1 of type union data and assign a value to its integer member. We then call the print_data function twice with d1 and pass a different type parameter each time.

Conclusion:

In this tutorial, we have covered what unions are, how to create them, and how to use them in your C programming code with practical examples and explanations. By understanding unions, you can create more flexible data structures that can store different types of data in the same memory space. Improve your programming skills today by experimenting with unions in your code.

 

Explore Tech Guide:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com