Course Content

  • unions

Course Content


In the C programming language, a union is a user-defined datatype. It is a memory region that contains a collection of variables of various datatypes. We can define a union with numerous members, but only one member can contain a value at any given time. Memory is saved by using C unions.

All members of a union have access to the same memory location. In the C programme below, for example, both x and y have the same position. We can see how changes in x are mirrored in y when we adjust x. #include stdio.h>/ Structures and declarations of union are the same thing.

The most typical purpose of a union is to allow various data types to access a single location, such as hardware input/output access, bitfield and word sharing, or type punning. Low-level polymorphism can also be found in unions.

When we define the union, we discover that it is defined in the same way that the structure is defined, with the exception that the union data type is specified using the union keyword, whereas the structure is defined using the struct keyword.

When we define a union, the memory allocated for a union variable of the type is the same as the memory required for the union's largest member, and other members share this memory space. "char arr[8]" is the largest member in the example above. As a result, the size of the union test is 8 bytes.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere