0% found this document useful (0 votes)
41 views3 pages

Printtime (Mytime - Percisedate) - .

A union allows the same storage location to be interpreted in different ways. Only one interpretation is valid at a time. Unions save memory by allowing a single variable to be accessed as different data types depending on current needs, but only one data type can be accessed at any one time as the members share the same memory location and overwrite each other. The size of a union is determined by its largest member type.

Uploaded by

Bhanu Karthik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

Printtime (Mytime - Percisedate) - .

A union allows the same storage location to be interpreted in different ways. Only one interpretation is valid at a time. Unions save memory by allowing a single variable to be accessed as different data types depending on current needs, but only one data type can be accessed at any one time as the members share the same memory location and overwrite each other. The size of a union is determined by its largest member type.

Uploaded by

Bhanu Karthik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

DEFINITION: A union is a collection of variables of different types, just like a structure.

However, with unions, you can only


store information in one field at any one time. A union is like a chunk of memory that is used to store variables of different types.
Once a new value is assigned to a field, the existing data is wiped over with the new data.

A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds
one of them at a time (not like a structure). This can save memory if you have a group of data where only one of the types is used
at a time.

The size of a union is equal to the size of its largest data member.

ACCESSING UNION FIELDS:

To access the fields of a union, use the dot operator(.) just as you would for a structure. When a value is assigned to one member,
the other member(s) get whipped out since they share the same memory. Using the example above, the precise time can be
accessed like this:

...
printTime( mytime.perciseDate );
. . . 

Summary 

Union allows same storage to be referenced in different ways 

only one way is valid at any given time 

Usage

 access individual bytes of larger type

 variable format input records (coded records)

 sharing an area to save storage usage

 unions not used nearly as much as structures

 size of union is size of its biggest member

 Unions most often contain different types of structures

 Can only initialize first member of union

 Can assign (copy) one union variable to another

 Can pass union or pointer to union as function arg

 Function can return union type

 Can define pointers to union type object

 Syntax, format and use of tags and declarators like struct, but members overlay each other, rather than following each
other in memory
 Initialization
 Union may only be initialized to a value appropriate for the type of its first member
#include <stdio.h>

int main()
{
union data
{
char a;
int x;
float f;
} myData;

int mode = 1;

myData.a = 'A';
printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f );

myData.x = 42;
mode = 2;
printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f );

myData.f = 101.357;
mode = 3;
printf("Here is the Data:\n%c\n%i\n%.3f\n", myData.a, myData.x, myData.f );

if( mode == 1 )
printf("The char is being used\n");
else if( mode == 2 )
printf("The int is being used\n");
else if( mode == 3 )
printf("The float is being used\n");
return 0;
}

You might also like