-->

17. C-Union

C-Structures: Previous                                                               Next: C-Storage Class
It is also user defined data type, which is used to store collection of variables having a different data type. Structure and Union are totally similar to one another expect that Union`s members share same memory.
                Syntax:    union
                                {
                                                ;
                                                ;
                                                ----------------------;
                                                ----------------------;
                                                ----------------------;
                                                ;
                                };
Two basic applications for Union in c programming are: -
  • Utilize the same memory location in many different ways.
  • To create flexible structure.
“Union holds a one data type value at time in memory and that value is equal to larger storage of their members.”
Eg:           union student
                {
                                int rollno;
                                char name[20];
                };
Memory will store by union in 20 bytes i.e. of ‘name’ data member.
Members of union can be accessed by using ( . ) dot operator, and initialization and declaration of union variable is same as in structure.
Eg:           #include
                                void main( )
                                {
                                                union student
                                                {
                                                                int rollno;
                                                                char name[20];
                                                                float fee;
                                                };
                                                union student std;
                                                std.rollno = 101;
                                                std.name = “Kareena”;
                                                std.fee = 10,000;
                                                printf(“\n The roll number is : ”,std.rollno);
                                                printf(“\n The Name is : ”,std.name);
                                                printf(“\n The Fees is : ”,std.fee);
                                }

Difference between Structure and Union: -


StructureUnion
All the data members share separate memory.Members of union data type share same memory.
‘struct’ keyword is used to declare the structure.‘union’ keyword is used to declare the Union.
It occupies large space in memory.It occupies less space in memory.

Enumerated data type [enum]: -
C provides some built in data types like int, float apart from that C also provides facility to define your own data type called “enumerated”. It is purely for numeric and used to give integer constant values to means. It defines special data items in form of numeric constants and gives ordered series to variable [data members].
Eg: Suppose we have 12 months, we can assign the integer constants to each and every month sequentially. So we don`t need to write the name of month rather integer constants can be used at their place. ‘enum’ keyword is used.
Syntax:    enum
{
Member list;
};
Eg:           enum week
{
Monday, Tuesday, Wednesday, Friday;
};
Declaration of variables in C programming: -

Eg:           week w;
In the previous example the ‘Monday’ is equivalent to 0, and ‘Tuesday’ is 1 and so on.
We can declare more than one variable of same enum type.
Eg:           week w, e;
This both variables when compared will give the result euqal.
Eg:           w = Monday;
e = Tuesday;
if(w == e)
printf(“Both are Equal”);
When we write any of the defined enumeration (Sunday, Monday, Tuesday) with printf( ) statement they will print integer constant, so the format specifier is %d.
Eg:           void main( )
{
printf(“%d”, Tuesday);            1
printf(“%d”, Thursday);          4
}

C-Structures: Previous                                                               Next: C-Storage Class