Technology has advanced to the point where programming has become an essential part of our lives. We use software and applications for communication, entertainment, education, and work, and these are all built on programming languages.

When you use structure and union in C ?

Structure is a user defined data type which hold or store homogeneous data item or element in a single variable. It is a Combination of primitive and derived data type.

In C language , array is also a user defined data type but array hold or store only similar type of data, If we want to store different type of data, then we need to define separate variable for each type of data. For this problem, Structure is best suit.

 

A union is quite similar to the structures in C. It also stores different data types in the same memory location. It is also a user defined data type same like structure. It is more near to structure. Union can be defined in same manner as structures, for defining union, use union keyword where as for defining structure, use struct keyword. 

When you need to manipulate the data for all member variables then use structure. When need to manipulate only on member then use union.

All the properties of the structure are same for union, except initialization process.

In case of structure initialize all data members at a time because memory locations are different but in case of union only one member need to be initialize.

In case of union if we initialize multiple member then compiler will gives an error.


Share:

No comments:

Post a Comment

If you have any doubt, let me know...

Translate

Wikipedia

Search results