STRING & UNION IN C

📚 Topics Covered

WHAT IS A STRUCTURE?

DEFINITION:

A structure is a user-defined datatype which is used to store multiple variables under a single name.
To create a structure we use struct keyword for creating it.

real life example of a variable

In the above image, the representation of structure is displayed in form of visuals.

SYNTAX:


struct structureName
{
	datatype variable1;
	datatype variable2;
	datatype variable3;
	.
	.
}structure_variable;
    

EXAMPLE:


struct Person
{
	char fullName[50];
	int age;
	int salary;
}p1;	
    

In the above example, Person structure is created which consists of multiple members (variables) of different datatype.
To access the members of the structure first we need to create the structure variable to access.

HOW TO ACCESS THE MEMBERS OF THE STRUCTURE?

To access the members of the function we use member operator ( . ) along with structure variables to access the members of structure.


structure_variable.membername = value;
    

EXAMPLE:


#include stdio.h

struct Person
{
	char fullName[50];
	int age;
	int salary;
}p1;

int main()
{
	printf("Enter full name: ");
	gets(p1.fullName);

	printf("Enter age: ");
	gets(p1.age);
	
	printf("Enter salary: ");
	gets(p1.salary);

	printf("\nPERSON DETAILS:\n\n");
	printf("Name: ");
	puts(p1.fullName);
	printf("Age: %d\n",p1.age);
	printf("Salary: %d\n",p1.salary);

	return 0;
}
    

In the above program, we have created p1 structure variable which stores fullname, age, salary for p1 using member operator.


Enter full name: Intelle Learn
Enter age: 21
Enter salary: 50000

We can create as many structure variables as we want for the structure & for each structure variable different memory is allocated for each.

WHAT IS A UNION?

DEFINITION:

A union is a user-defined datatype which is used to store multiple variables under a single name.
To create a union we use union keyword for creating it.

SYNTAX:


union unionName
{
	datatype variable1;
	datatype variable2;
	datatype variable3;
	.
	.
}union_variable;

    

EXAMPLE:


#include stdio.h

union Person
{
	char fullName[50];
	int age;
	int salary;
}p1;

int main()
{
	printf("Enter full name: ");
	gets(p1.fullName);

	printf("Enter age: ");
	gets(p1.age);
	
	printf("Enter salary: ");
	gets(p1.salary);

	printf("\nPERSON DETAILS:\n\n");
	printf("Name: ");
	puts(p1.fullName);
	printf("Age: %d\n",p1.age);
	printf("Salary: %d\n",p1.salary);

	return 0;
}
    

DIFFERENCE BETWEEN STRUCTURE & UNION

What is the Difference?

In the above example the size of the structure is 58 bytes as it calculates the memory of each datatype.
Whereas in UNION it will select the largest element size of the Union as the memory of the Union hence 50 bytes.

Video Explanation:

If video not loading,watch on YouTube

Practice Task

Write a program to take full the details of a student (Roll no, Student name, Class, Division) and display the details on the screen.

Challenge

Try to create your own mini project so that you will understand what you have truly learnt about C.