0% found this document useful (0 votes)
14 views

Unit-4 Union

The document discusses unions in C programming. Unions allow storing different data types in the same memory location. Only one member can contain a value at a time. The document provides the syntax for defining unions and accessing union members. It also compares unions to structures and provides an example program to add two time periods represented as a union.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Unit-4 Union

The document discusses unions in C programming. Unions allow storing different data types in the same memory location. Only one member can contain a value at a time. The document provides the syntax for defining unions and accessing union members. It also compares unions to structures and provides an example program to add two time periods represented as a union.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

USING

Union {
C}
Programmin
g
What is Union?
Union is a user defined data type similar like Structure.
It holds different data types in the same memory location.
You can define a union with various members, but only one member can hold a
value at any given time.
Union provide an efficient way of using the same memory location for multiple-
purpose.
Syntax to Define and Access Union
Declaration of union must start with the keyword union followed by the union
name and union’s member variables are declared within braces.
Syntax
1 union union_name union_name is name of custom type.
2 {
3 member1_declaration;
4 member2_declaration;
memberN_declaration is individual member
5 ...
declaration.
6 memberN_declaration;
7 };
Accessing the union members:
You need to create an object of union to access its members.
Object is a variable of type union. Union members are accessed using the dot operator(.)
between union’s object and union’s member name.
Syntax
1 union union_name union_variable;
Example to Define Union

Example
1 union student
2 {
3 char name[30]; // Student Name
4 int roll_no; // Student Roll No
5 float CPI; // Student CPI
6 int backlog; // Student Backlog
7 } student1;

You must terminate union definition with semicolon ;.


You cannot assign value to members inside the union definition, it will cause
compilation error.
Example
1 union student
2 {
3 char name[30] = “ABC”; // Student Name
4 ...
5 } student1;
Structure Vs. Union

COMPARISON STRUCTURE UNION


Basic The separate memory location is allotted to All members of the 'union' share the same memory location.
each member of the structure.
keyword 'struct' 'union'
Size Size of Structure = sum of size of all the data Size of Union = size of the largest member.
members.
Store Value Stores distinct values for all the members. Stores same value for all the members.
At a Time A structure stores multiple values, of the A union stores a single value at a time for all members.
different members, of the structure.
Declaration struct ss union uu 4 bytes
{ 1 byte for c {
int a; 2 bytes for a int a; c
float f; float f;
a
4 bytes for f f
char c char c
}; };
Write a program to declare time structure and read two different time period and display sum of it.

WAP to print Odd numbers between 1 to n


Program
1 #include<stdio.h> 27 scanf ("%d",&t2.seconds);
2 union time { 28 printf ("The Time is
3 int hours; 29 %d:%d:%d",t2.hours,t2.minutes,t2.seconds);
4 int minutes; 30 h = t1.hours + t2.hours;
5 int seconds; 31 m = t1.minutes + t2.minutes;
6 }; 32 s = t1.seconds + t2.seconds;
7 int main() { 33 printf ("\nSum of the two time's is
8 union time t1,t2; 34 %d:%d:%d",h,m,s);
9 int h, m, s; 35 return 0;
10 //1st time 36 }
11 printf ("Enter 1st time."); 37
12 printf ("\nEnter Hours: ");
13 scanf ("%d",&t1.hours);
14 printf ("Enter Minutes: "); Output
15 scanf ("%d",&t1.minutes); Enter 1st time.
16 printf ("Enter Seconds: "); Enter Hours: 1
17 scanf ("%d",&t1.seconds); Enter Minutes: 20
18 printf ("The Time is %d:%d:%d",t1.hours,t1.minutes,t1.seconds); Enter Seconds: 20
19 //2nd time The Time is 1:20:20
20 printf ("\n\nEnter the 2nd time.");
21 printf ("\nEnter Hours: "); Enter the 2nd time.
22 scanf ("%d",&t2.hours); Enter Hours: 2
23 printf ("Enter Minutes: "); Enter Minutes: 10
24 scanf ("%d",&t2.minutes); Enter Seconds: 10
25 printf ("Enter Seconds: "); The Time is 2:10:10
26 Sum of the two time's is 3:30:30
Where Union should be used?
Mouse Programming
Embedded Programming
Low Level System Programming

Thank you

You might also like