Unions in C Programming

Unions are a concept of structures and therefore follow the same syntax as structures.however,there is major distinction between them in terms of storage.in structures,each member has its own storage location,whereas all the members of a union use the same location.

a union can be declared using the keyword union as follow the syntax of union.

Syntax


union tag_name
{
	member;
}union variables;							
							

Example


#include <stdio.h>
union student
{
   char name[32];
}u;

void main()
{
   printf("Enter Student Name:\n");
   scanf("%s",&u.name);
   printf("Student Name: %s",u.name);
   
   getch();
}							
							

Output


Enter Student Name:Johan
Student Name: Johan							
							
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!