← Back to C Course | Chapter 8: Structures & Unions | Lesson 5 of 7

C Unions

A union is like a single box that can hold one of several kinds of things, but only one at a time. It saves memory by sharing the same space.
Syntax
c
union UnionName {
  data_type member1;
  data_type member2;
};

union UnionName variable_name;
variable_name.member1 = value;

What is a Union?

This shared-memory design makes unions much more memory-efficient than structures when you only ever need one of several possible values at a time -- for example, representing a value that could be an int, a float, or a string, but never more than one simultaneously.

Example: What is a Union?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
	int i;
	float f;
};
// Program execution starts in main()
int main() {
	union Data d;
	d.i = 5;
	// Print formatted text to the screen
	printf("%d", d.i);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Defining a Union

This is syntactically identical to defining a structure, but the compiler's memory layout is fundamentally different: every member starts at the same base address rather than being laid out sequentially.

Example: Defining a Union

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Value {
	int i;
	char c;
};
// Program execution starts in main()
int main() {
	union Value v;
	v.i = 65;
	// Print formatted text to the screen
	printf("%d", v.i);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Memory Allocation in Unions

If your union has an int (4 bytes) and a double (8 bytes), the union's total size is 8 bytes -- exactly enough for its largest member, since all members overlap the same memory region rather than being stacked.

Example: Memory Allocation in Unions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
	int i;
	double d;
};
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu", sizeof(union Data));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Accessing Union Members

Writing to one member and then reading a different member reinterprets the same raw bytes as a different type, which is sometimes done deliberately (called 'type punning') but is usually a bug if done accidentally.

Example: Accessing Union Members

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
	int i;
	float f;
};
// Program execution starts in main()
int main() {
	union Data d;
	d.i = 10;
	d.f = 3.5f;
	// Print formatted text to the screen
	printf("%.1f", d.f);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Unions vs. Structures

Choose a structure when you need to store several related values simultaneously (like a point's x and y), and a union when you need to represent one value that could be several different types depending on context (like a variant/tagged data type).

Example: Unions vs. Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x, y; };
union Value { int i; float f; };
// Program execution starts in main()
int main() {
	struct Point p = {1, 2};
	union Value v;
	v.i = 5;
	// Print formatted text to the screen
	printf("%d %d %d", p.x, p.y, v.i);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reading a union member other than the one last written, which gives unreliable results.
  2. Assuming all union members hold values at once.
  3. Assuming a union's size is the sum of its members, when it is the size of the largest.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.