Union in C Programming with Examples
/*
* C program to illustrate the concept of unions using dot operator
*/
#include<stdio.h>
int main()
{
#include <stdio.h>
//scope of union -> main function
void main()
{
union number
{
int n1;
float n2;
};
//initializing union number variable by using union keyword and tag name
union number x;
printf("Enter the value of n1: ");
scanf("%d", &x.n1); //access using dot product
printf("Value of n1 = %d", x.n1);
printf("\nEnter the value of n2: ");
scanf("%f", &x.n2);
printf("Value of n2 = %f\n", x.n2);
}
Output
Enter the value of n1: 10
Value of n1 = 10
Enter the value of n2: 50
Value of n2 = 50.000000
