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

 

Learn to Build 5 Apps Without Coding using FlutterFlow

  • Lifetime access to all the 5 FlutterFlow Courses
  • Complete FlutterFlow training
  • All future course updates
  • Access via Codzify mobile app
  • 24h 42m of Learning Content
  • 5 FlutterFlow at Codzify just ₹1500 ($17.44)
  • Access to Dating App Course, Grocery App Course, Courses App Course, FlutterFlow AI Agents Course, Doctor Appointment Booking Supabase Course.
  • Clone Ready-Made Courses App instantly in FlutterFlow.
  • 30-Day 100% Money-Back Guarantee.

Learn More
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: [email protected]