Union in C

In this section of C programming tutorial, we will have a look at the union. Like Structures, A union is a special data type available in C. However, in union, all members share the same memory location.

Syntax of Union in C

union union_name
data_type member1;
data_type memeberN;

Example of Union in C

#include <stdio.h>
#include <string.h>
union student
int rollno;
char name[60];
}s1;  //declaring s1 variable for union
void main( )
//store first employee information
strcpy(s1.name, “John”);//copying string into char array
//printing first employee information
printf( "Rollno : %d\n", s1.rollno);
printf( "Name : %s\n", s1.name);

Rollno : 234
Name : John

Leave a Reply

Your email address will not be published. Required fields are marked *