You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h3>Unions in C: Memory-Efficient Type Sharing:</h3>
2648
+
<p>A union is a special user-defined data type that can hold members of different data types, but only one member can hold a value at any given time. All members of a union share the same memory location, making them highly memory-efficient.</p>
2649
+
</div>
2650
+
<divclass="content-block">
2651
+
<h3>Syntax:</h3>
2652
+
<pre><code>// Union declaration
2653
+
union Data {
2654
+
int i;
2655
+
float f;
2656
+
char str[20];
2657
+
};</code></pre>
2658
+
</div>
2659
+
<divclass="content-block">
2660
+
<h3>Example:</h3>
2661
+
<pre><code>#include <stdio.h>
2662
+
2663
+
// Define a union to store different types of data
2664
+
union Value {
2665
+
int intVal;
2666
+
float floatVal;
2667
+
char charVal;
2668
+
};
2669
+
2670
+
int main() {
2671
+
union Value val;
2672
+
2673
+
// Assign and print integer
2674
+
val.intVal = 100;
2675
+
printf("Integer value: %d\n", val.intVal);
2676
+
2677
+
// Assign and print float (overwrites int)
2678
+
val.floatVal = 3.14;
2679
+
printf("Float value: %.2f\n", val.floatVal);
2680
+
2681
+
// Assign and print char (overwrites float)
2682
+
val.charVal = 'A';
2683
+
printf("Char value: %c\n", val.charVal);
2684
+
2685
+
// Show memory size
2686
+
printf("Size of union: %lu bytes\n", sizeof(val));
2687
+
2688
+
return 0;
2689
+
};</code></pre>
2690
+
</div>
2691
+
<divclass="content-block">
2692
+
<h3>Output:</h3>
2693
+
<code>Integer value: 100
2694
+
Float value: 3.14
2695
+
Char value: A
2696
+
Size of union: 4 bytes</code>
2697
+
</div>
2698
+
<divclass="quiz-container">
2699
+
<h3>C Unions Basics:</h3>
2700
+
<divclass="question-block">
2701
+
<p>1. What is a key characteristic of a C union regarding its members and memory allocation?</p>
2702
+
<divclass="options">
2703
+
<divclass="quiz-option" data-question="q1" data-value="a">A. All members have separate memory locations.</div>
2704
+
<divclass="quiz-option" data-question="q1" data-value="b">B. It can hold multiple member values simultaneously.</div>
2705
+
<divclass="quiz-option" data-question="q1" data-value="c">C. All members share the same memory location, and only one member can hold a value at a time.</div>
2706
+
<divclass="quiz-option" data-question="q1" data-value="d">D. Its size is the sum of the sizes of all its members.</div>
2707
+
</div>
2708
+
<divclass="quiz-feedback"></div></div>
2709
+
2710
+
<divclass="question-block">
2711
+
<p>2. Which of the following correctly declares a union named <strong>MyUnion</strong> with an integer member "x" and a float member "y"?</p>
2712
+
<divclass="options">
2713
+
<divclass="quiz-option" data-question="q2" data-value="a">A. MyUnion { int x; float y; };</div>
2714
+
<divclass="quiz-option" data-question="q2" data-value="b">B. union MyUnion { int x; float y; };</div>
0 commit comments