-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC Unions.c
More file actions
29 lines (21 loc) · 742 Bytes
/
C Unions.c
File metadata and controls
29 lines (21 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
// Define a union to represent a variable that can be either an //integer or a float
union Number {
int intValue;
float floatValue;
};
int main() {
// Declare a union variable
union Number num;
// Store an integer value in the union
num.intValue = 42;
// Access and display the integer value
printf("Integer value: %d\n", num.intValue);
// Store a float value in the union
num.floatValue = 3.14;
// Access and display the float value
printf("Float value: %.2f\n", num.floatValue);
// Access and display the integer value after storing a float value
printf("Integer value after storing a float: %d\n", num.intValue);
return 0;
}