-->
Showing posts with label difference between struct and union. Show all posts
Showing posts with label difference between struct and union. Show all posts

NEB30:Difference between struct and union

Ans:-
The differences between struct and union are given below in tabular form.
                   
struct
Union
1.It stores dis-similar data.Each gets separate space in memory.They do not share memory
1. It stores dis-similar type of data.Total memory space is equal to the member with largest size.They share memory space.
2.we can access member in any sequence .
2.We can access that whose value is recently used.
3.It uses keyword 'struct'
3.It uses keyword 'union'
4.We can initialize values of member/s and in any order
4.We can initialize that member which is first declared.
5.It consumes more memory
5.It consumes less memory.
6.Its syntax
    struct tag
{
datatype member1;
datatype member2;
datatype member3;
}variable;

6.Its syntax
    union tag
{
datatype member1;
datatype member2;
datatype member3;
}variable;

Example
struct student
{
int roll;
char sex;
float percentage;
}var;

Example
union student
{
int roll;
char sex;
float percentage;
}var;