A union is a block of memory that is used to hold data items of different types. In C, a union is similar to a structure,
except that data items saved in the union are overlaid in order to share the same memory location. The syntax for
declaring a union is similar to the syntax for a structure. The general form to declare a union is
union union_tag {
data_type1 variable1;
data_type2 variable2;
data_type3 variable3;
.
.
.
};
Here union is the keyword used in C to start a union declaration. union_tag is the tag name of the union. variable1 ,
variable2, and variable3 are the members of the union. Their data types are specified respectively by data_type1,
data_type2, and data_type3. The union declaration is ended with a semicolon (;).
The following is an example of a union declaration:
union automobile {
int year;
char model[8];
int engine_power;
float weight;
};
Here union specifies the union data type. automobile is the tag name of the union. The variables, such as year, model,
engine_power, and weight, are the members of the union and are declared within the braces ({ and }).
Friday, October 16, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment