Structure and Union in C are the user-defined types which used to store the collection of different data items.

Structure and Union in C

Structure and union are user-defined data type in c programming. Both special data types are used to store to combine different data items into a single entity.
Let’s see structure and union in C one by one.

Structure in C

A Structure is a user-defined data type.
The structure is a collection of elements of a different type. It is used to store values of different type of variable into a single type. The structure is used to represent the record. It is similar to an array, Array holds similar data items whereas structure holds different data items.

Declaration of structure
Syntax : –

struct <struct_name>

{

<Datatype>. member1;

<Datatype>. Member2;

.

<Datatype>. Member n;

};

In the above syntax, a struct is a keyword and <struct_name> is a name given to the structure and in the structure we declare different types of variable.
Memory allocated to the structure is according to the member in the structure.
Size of the structure is equal to the sum of memory allocated to all members in the structure.

We create a struct object as follows
struct <Struct_name> variblr_name;

Accessing structure members
We can access members of structure using dot operator which is shown below
<struct_variable> . Member_name;

Array of structure

We can declare an array of a structure variable. We can create an array of a structure as follows
Syntax:-
struct <struct_name> array_name[size];

We can access the individual member of a structure by a[i] where a is array name and i is index.

Structure with pointer

A pointer variable can be used in structure to point and process that data.
To access the structure member the arrow operator is used as ->
Syntax :- <structure_pointer> -> structure_member;

Structure example: –

To store student information
struct student{
int roll_no;
char name[50];
};

Why structure is used in C?

A structure is special a data type that can be used to store items of different types into a single type. ‘struct’ keyword is used to create a structure. Following is an example. A structure variable can either be declared with a structure declaration or as a separate declaration like basic types.

Program to illustrate structure in c

#include <stdio.h>

// Declaration of structure is same as structures
struct student

{

int no;

char name[30];

};


int main()
{
// A structures variable t
struct student s;

printf("Enter Number:");

scanf("%d",&s.no);

printf("Enter Name:");

scanf("%s",&s.name);

printf("\n************************\n");

printf("\nNumber : %d",s.no);

printf("\nName : %s",s.name);

return 0;
}

Output:-

Structure in CUnion in C

Like a structure Union also is a user-defined data type. A union is a special data type used to store the elements of different types. In union, all members share the same memory location.
For example  – Both x and y share the same location. If we change x, we can see the changes being reflected in y.
Difference between the structure and union is, in structure all members in structure has its memory location but in union members use the same locations for storage.

Union Syntax :-

union <union_name>

{

<Datatype>. member 1;

<Datatype>. Member 2;

.

.

<Datatype>. Member n;

} Variable_name;

Union example: –

union ex{
int value1;
int value2;
};

Union program in c

#include <stdio.h>

// Declaration of union is same as structures
union test {
int A, B;
};

int main()
{
// A union variable t
union test t;

t.A = 2; // t.B also gets value 2
printf("After making A = 2:\n A = %d, B = %d\n\n",
t.A, t.B);

t.B = 10; // t.A is also updated to 10
printf("After making B = 10:\n A = %d, B = %d\n\n",
t.A, t.B);
return 0;
}

Output: –

Union in C

What is use of union in C?

A union is a special data type in C programming that allows different data types to be stored in the same memory locations. Union provides an efficient way of reusing the memory location, as only one of its members can be accessed at a time. A union is used almost in the same way you would declare and use a structure.

What is the difference between structure and union in c?

Both the data types are used to store different data items in a single type. The main difference between structure and union is, structure uses different memory location to store its each structure members whereas union uses the same memory location for its members.
Consider Following example: – Structure and union with two members
Struct test{
int x;
float y;
}object;

similarly Union with two members,

struct test{
int x;
float y;
}object;

Structure and Union in C
Structure and Union in C

The above diagram shows the memory representation for both structure and union where we can see that structure contains different memory for x and y whereas union uses the same memory for x and y.

The following table shows all the differences between structure and union in C

Difference between Structure and union in C
Difference between Structure and union in C

Write A Comment