Tutorial references

c
c++
c#

Tutorial references

chapters

Union

Union

Union is a user define data-type in C language. Union holds different type elements.

Each element of union is called member.

Union occupies space in memory only for the largest member not for all members.

It holds data only in occupied memory of the largest member.

NOTE:- Only one member of union can be accessed at a time.

Union syntax:

union union_name{
data-type member-variable-1;
data-type member-variable-2;
data-type member-variable-3;
...........
...........
};

union is a keyword.

union_name is name of union. This name is given by programmers.

Example:

Example

union student{
char stName[100];
char stMob[15];
char stAddress[300];
};

In above example "student" is the name of union. stName, stMob, stAddress are member of union.

Declaring Union Variable

There are two ways to declare union variable.

  (1.) with union body.

  (2.) In main() function.

1st-way(with union body) Syntax:

union union_name{
data-type member-variable-1;
data-type member-variable-2;
data-type member-variable-3;
...........
...........
}union_variable1, union_variable2,......;

Example:

union student{
char stName[100];
char stMob[15];
char stAddress[300];
}st1,st2;

In above example st1 and st2 are union type variable.

2nd-way(in main() function) Syntax:

union structure-name variable;

Example

union student st1,st2;

Accessing Member of Union

There are two ways to access union members:

   (1)By . (member or dot operator)

   (2)By -> (pointer operator)

Example

#include<stdio.h>
#include<string.h>
#include<conio.h>

union student{
char stName[100];
char stMob[15];
char stCourse[50];
}st1,st2;

void main(){
clrscr();
puts("Enter name of student:");
gets(st1.stName);
puts("Enter mobile number of student:");
gets(st1.stMob);
puts("Enter course of student:");
gets(st1.stCourse);/* Union will return value in output only for this statement because stCourse variable read at last.*/
printf("\n******************************\n");
puts("Enter name of student:");
gets(st2.stName);
puts("Enter mobile number of student:");
gets(st2.stMob);
puts("Enter course of student:");
gets(st2.stCourse);/* Union will return value in output only for this statement because stCourse variable read at last.*/
printf("\n========student details==========");
printf("\nCourse of student: ");
printf(st1.stCourse);
printf("\n--------------------------------");
printf("\nCourse of student:");
printf(st2.stCourse);
getch();
}

Run program

Example

#include<stdio.h>
#include<string.h>
#include<conio.h>

union student{
char stName[100];
char stMob[15];
char stCourse[50];
};

void main(){
union student st1,st2;
clrscr();
puts("Enter name of student:");
gets(st1.stName);
puts("Enter mobile number of student:");
gets(st1.stMob);
puts("Enter course of student:");
gets(st1.stCourse);/* Union will return value in output only for this statement because stCourse variable read at last.*/
printf("\n******************************\n");
puts("Enter name of student:");
gets(st2.stName);
puts("Enter mobile number of student:");
gets(st2.stMob);
puts("Enter course of student:");
gets(st2.stCourse);/* Union will return value in output only for this statement because stCourse variable read at last.*/
printf("\n========student details==========");
printf("\nCourse of student: ");
printf(st1.stCourse);
printf("\n--------------------------------");
printf("\nCourse of student:");
printf(st2.stCourse);
getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018