Sunday, March 5, 2017

Difference between Structure and Union

Structure: structure in C language is a collection of variables with a single name. These variables can be of different types. 

Union: It is a special data type in C which allows us to store different data types in the same memory location.


 Structure

 Union
struct keyword is used to define Structure in C.

union keyword is used to define Union in C. 
Members of structure do not share memory. 

Members of union shares the memory. 

 Members of structure can be accessed at any time individually.

At a time only one member of union can be accessed.
Syntax:

struct structure_name
{
  datatype var1;
  datatype var2;
  ---
  ---
  datatype varn;

}struct_variable_name;
Syntax:

union union_name
{
  datatype var1;
  datatype var2;  ---
  ---
  datatype varn;

}union_variable_name; 


No comments:

Post a Comment