Union

Union in C

Union in C

Union in C is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.

  1. What is union with example?
  2. What is union and structure in C?
  3. When should I use union in C?

What is union with example?

Like Structures, union is a user defined data type. In union, all members share the same memory location. For example in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y.

What is union and structure in C?

A structure contains an ordered group of data objects. Unlike the elements of an array, the data objects within a structure can have varied data types. Each data object in a structure is a member or field. A union is an object similar to a structure except that all of its members start at the same location in memory.

When should I use union in C?

C unions are used to save memory. To better understand an union, think of it as a chunk of memory that is used to store variables of different types. When we want to assign a new value to a field, then the existing data is replaced with new data.

Deselectable radio button or limited checkbox in search filters
In which situation should you use a radio button or checkbox?Why would you choose a radio button instead of a checkbox?Which is better radio buttons ...
Show Related Items
What is a related product display?What are the related products? What is a related product display?Related products are products that connect to the...
Contextual menu vs. Right-click menu. Is the term contextual menu misused?
What is the right-click menu also called?What are contextual menu options?What is context menu example?What is a contextual menu on a Mac? What is t...