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

C के Unions

एक union एक single box जैसा है जो कई तरह की चीज़ों में से एक रख सकता है, लेकिन एक बार में सिर्फ एक। यह same space share करके memory बचाता है।
Syntax
c
union UnionName {
  data_type member1;
  data_type member2;
};

union UnionName variable_name;
variable_name.member1 = value;

Union क्या है?

यह shared-memory design unions को structures से कहीं ज़्यादा memory-efficient बनाता है जब आपको किसी भी समय कई possible values में से सिर्फ एक चाहिए हो -- उदाहरण के लिए, एक value represent करना जो एक int, एक float, या एक string हो सकती है, लेकिन एक साथ एक से ज़्यादा कभी नहीं।

उदाहरण: 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;
}

एक Union Define करना

यह syntactically एक structure define करने जैसा ही है, लेकिन compiler का memory layout fundamentally अलग है: हर member sequentially lay out होने की बजाय same base address से शुरू होता है।

उदाहरण: 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;
}

Unions में Memory Allocation

अगर आपके union में एक int (4 bytes) और एक double (8 bytes) है, union का total size 8 bytes है -- इसके सबसे बड़े member के लिए exactly काफी, क्योंकि सारे members stacked होने की बजाय same memory region overlap करते हैं।

उदाहरण: 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;
}

Union Members Access करना

एक member में लिखना और फिर एक अलग member पढ़ना same raw bytes को एक अलग type की तरह reinterpret करता है, जो कभी-कभी जानबूझकर किया जाता है (जिसे 'type punning' कहते हैं) लेकिन accidentally होने पर आमतौर पर एक bug है।

उदाहरण: 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 बनाम Structures

एक structure चुनें जब आपको एक साथ कई related values store करनी हों (जैसे एक point का x और y), और एक union जब आपको एक value represent करनी हो जो context के आधार पर कई अलग types में से एक हो सकती है (जैसे एक variant/tagged data type)।

उदाहरण: 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. #}
आम गलतियां
  1. आखिरी बार लिखे गए के अलावा एक union member पढ़ना, जो unreliable results देता है।
  2. यह मान लेना कि सारे union members एक साथ values रखते हैं।
  3. यह मान लेना कि एक union का size इसके members का sum है, जब यह सबसे बड़े का size है।
🔒

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.