0 votes
126 views
in C programming by (98.9k points)
edited
Comparison between union and structure

1 Answer

0 votes
by (98.9k points)
edited by
 
Best answer
S.no
C Structure
C Union
1
Structure allocates storage space for all its members separately.

Union allocates one common storage space for all its members.

Union finds that which of its member needs high storage space over other members and allocates that much space
2
Structure occupies higher memory space.
Union occupies lower memory space over structure.
3
We can access all members of structure at a time.
We can access only one member of union at a time.
4

Structure example:

struct student
{
int mark;
char name[6];
double average;
};

Union example:

union student
{
int mark;
char name[6];
double average;
};
 5

For above structure, memory allocation will be like below.

int mark – 2B
char name[6] – 6B
double average – 8B
Total memory allocation = 2+6+8 = 16 Bytes

For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types.

Total memory allocation = 8 Bytes

Related questions

0 votes
1 answer 110 views
asked Jun 11, 2022 in C programming by Doubtly (98.9k points)
0 votes
1 answer 138 views
0 votes
1 answer 113 views
0 votes
1 answer 147 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

561 users

...