Difference between structure and union in c programming language

Difference between structure and union in c

Structure

Structure is a user defined datatype, programmer can pack up one or more variables of other datatypes into a single element.

struct keyword is used to define a structure.

Memory is allocated to all structure members.

Dot ( . ) operator is used to access a structure member. Arrow operator is used to access structure member via pointer.

Structure supports sizeof and assignment (=) operators.

Altering a member’s value doesnt affect value of other members.

Read more on c structures.

syntax

struct struct_name
{ 
 member definition;
 member definition;
 member definition;
 .
 .
 .
 .
 .
 member definition;
};

Struct example program

#include<stdio.h>
int main(int argc, char const *argv[])
{
	struct my_struct
	{
		int variable1;
		int variable2;
	};
	struct my_struct example;
	example.variable1=100;
	example.variable2=20;
	printf("C Structures\nVariable 1 : %d\nVariable2 : %d",example.variable1,example.variable2);
	return 0;
}
C structure example

Union

Union allows programmer to pack up one or more variables of other data types but memory is allocated for size of largest data type member.

Union keyword is used to define a Union.

Memory is allocated for size of largest data type member.

Dot ( . ) operator is used to access a union member. Arrow operator is used to access union member via pointer.

Union supports sizeof and assignment (=) operators.

Altering a member’s value affects the value of other members.

Syntax

union union_name
{ 
 member definition;
 member definition;
 member definition;
 .
 .
 .
 .
 .
 member definition;
};

Union example program

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf("\nC  datatypes\nDerived Datatypes\n");	
	struct example_struct//this is structure
	{
		int num;
	};
	struct example_struct e;
	e.num=10;
	union example_union//this is union
	{
		int data;
		float pi;
	};
	union example_union un;
	un.pi=3.142;
	int array[2]={1,2};//this is array
	int  num=0;//this is int variable
	int *ptr=&num;//pointer pointing to num
	printf( " Struct variable: %d\nUnion Example: %f \nArray elements : %d %d\nPointer value %d\n",e.num,un.pi,array[0],array[1],*(ptr) );
	return 0;
}

Cheatsheet table of difference between structure and union in c

StructureUnion
Using structure programmer can pack up one or more variables of other datatypes into a single element.Union allows programmer to pack up one or more variables of other data types but memory is allocated for size of largest data type member.
struct is the keyword to define a structure variable.union is the keyword to define a union variable.
Altering a member’s value doesnt affect value of other members.Altering a member’s value affects the value of other members.
Memory allocated to all members of a structure.Memory is allocated for a member whose size is largest.
Syntax:
struct structure_name
{
member definition;
member definition;
member definition;
};
Syntax:
union union_name
{
member definition;
member definition;
member definition;
};
Difference between structure and union cheat sheet

People also ask for

Which is better structure or union?

Depends upon the situation. If you are running out of memory then choose union. If you have much memory and dont want to affect other member variables then prefer structure.


People also read


Leave a Comment