C langugae : Unions

A union is a memory location that is shared by two or more different types of variables. A union provides a way of interpreting the same bit pattern in two or more different ways. Declaring a union is similar to declaring a structure.
* Union and structure in C are same in concepts, except allocating memory for their members.
* Structure allocates storage space for all its members separately.
* Whereas, Union allocates one common storage space for all its members
* We can access only one member of union at a time. We can’t access all member values at the same time in union. But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Where as Structure allocates storage space for all its members separately.
* Many union variables can be created in a program and memory will be allocated for each union variable separately.
* Below table will help you how to form a C union, declare a union, initializing and accessing the members of the union.
Type Using normal variable Using pointer variable
Syntax union tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
union tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Example union student
{
int  mark;
char name[10];
float average;
};
union student
{
int  mark;
char name[10];
float average;
};
Declaring union variable union student report; union student *report, rep;
Initializing union variable union student report = {100, “Mani”, 99.5}; union student rep = {100, “Mani”, 99.5};report = &rep;
Accessing union members report.mark
report.name
report.average
report  -> mark
report -> name
report -> average

Example program for C union:
{
char name[20];
char subject[20];
float percentage;
}record;
int main()
{
strcpy(record.name,"Anuj");
strcpy(record.subject,"Computer Science");
record.percentage=72;
printf("Name : %s \n",record.name);
printf("Subject : %s \n",record.subject); printf("Percentage : %f \n",record.percentage);
return 0;
}
Name :
Subject :
Percentage : 86.500000

Note:

      We can access only one member of union at a time. We can’t access all member values at the same time in union. But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Where as Structure allocates storage space for all its members separately.


Bit-Fields
Members of structures or unions can also be bit-fields . A bit-field is an integer variable that consists of a specified number of bits. If you declare several small bit-fields in succession, the compiler packs them into a single machine word. This permits very compact storage of small units of information. Of course, you can also manipulate individual bits using the bitwise operators, but bit-fields offer the advantage of handling bits by name, like any other structure or union member.
The declaration of a bit-field has the form:
type [member_name] : width ;
* type :- An integer type that determines how the bit-field's value is interpreted. The type may be _Bool, int, signed int, unsigned int, or another type defined by the given implementation. The type may also include type qualifiers.
Bit-fields with type signed int are interpreted as signed; bit-fields whose type is unsigned int are interpreted as unsigned. Bit-fields of type int may be signed or unsigned, depending on the compiler.
* member_name : - The name of the bit-field, which is optional. If you declare a bit-field with no name, though, there is no way to access it. Nameless bit-fields can serve only as padding to align subsequent bit-fields to a certain position in a machine word.
* width: - The number of bits in the bit-field. The width must be a constant integer expression whose value is non-negative, and must be less than or equal to the bit width of the specified type.

Nameless bit-fields can have zero width. In this case, the next bit-field declared is aligned at the beginning of a new addressable storage unit.
struct Date { unsigned int month : 4; // 1 is January; 12 is December.
unsigned int day : 5; // The day of the month (1 to 31).
signed int year : 22; // (-2097152 to +2097151)
_Bool isDST : 1; // True if Daylight Saving Time is
// in effect.
};
A bit-field of n bits can have 2n distinct values. The structure member month now has a value range from 0 to 15; the member day has the value range from 0 to 31; and the value range of the member year is from -2097152 to +2097151.


Free Web Hosting