In C, the datatype is data transmitted between the developer and the compiler in which the developer informs the compiler about which type of data stored and how much space it needs in the memory.
Both Structure and Union are all User-defined data types which can hold data of any “type”, which can derive from the Primary data types. For storage of data of same type C offers the concept of Array, while for preserving distinct datatype in C has a theory of structure and union.
We’ll be covering the following topics in this tutorial:
Structures in C
A structure is a user-defined datatype in C that used to store a combination of logically related data items of different types of data into a single type. Structure helps to construct a complex datatype. It’s somewhat like an Array, but an array holds data of similar type only. Each element in a C structure is known as “member”. If you would like to access structure members in C, the structure variable declared. By default, all the members of a structure are “public”. All the structure members saved at nearby memory locations. The structure type variable can hold multiple data object of varying data types under a single name.
Structure Definition: The declaration of a structure is always preceded by the keyword “struct”, which informs the compiler that structure has declared. Structure members can get by using the dot(.) operator. The struct statement defines a new data type, with more or equal to a single member. The format of the struct statement is as follows:
Here is the syntax of structures in C language
struct [structure name] { member 1 definition; member 2 definition; ... member n definition; };
The structure declared with the “struct” keyword and name of the structure. Member 1, Member 2, Member n are individual members of the structure. The body area terminated with a semicolon(;).
Advantages of structure in C
Here are benefits for using structure:
• Structures collect more than one bit of data regarding the identical topic together in precisely the same location.
• it’s useful once you would like to accumulate the data of related data types and parameters such as first name, last name, etc..
• It is straightforward to keep as we can represent the entire record by using one name.
• In structure, we could pass a whole set of records into any function with one parameter.
• it’s possible to use an array of structure to save more records with similar types.
Disadvantages of structure
Here are drawbacks for using structure:
• Change of a single data structure in a code requires changes at a number of different places. Hence, the gaps become hard to monitor.
• Structure is slower because it requires storage space for all the data.
• it’s possible to retrieve any member at one time in structure although you are able to get single member at one time at the union.
• Structure occupies space for every member composed in inner parameters while union occupies space for a member having the highest size composed in internal parameters.
• Structure supports adaptive array. Union doesn’t support a flexible array.
Example of Structure in C Programming
#include<stdio.h> struct student { char name[60]; int roll_no; float marks; } sData; int main() { printf("Enter the Student record:\n"); printf("Enter name: "); fgets(sData.name, sizeof(sData.name), stdin); printf("Enter roll number: "); scanf("%d", &sData.roll_no); printf("Enter marks: "); scanf("%f", &sData.marks); printf("The Data you have entered is: \n"); printf("Student name: "); printf("%s", sData.name); printf("Student roll number: %d\n", sData. roll_no); printf("Student marks: %.1f\n", sData.marks); return 0; }
Output :
Union in C
A union is a user-defined data type in C which enables storing items of different types and sizes together at the same memory location. It’s possible to specify a union with several members, but one member can contain a value at any particular time. The keyword “union” utilized to define unions in C language. Union provides an efficient method of reusing the memory location, as only one of its members can access at a time.
Defining a Union: To define a union, you need to utilize the union statement in the same manner as you did while creating a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows:
How to Declare Union in C ?
• Union is similar to that of structure. Syntax of both would be the same; however, the significant difference between structure and union is ‘memory storage’.
• In structures, every member has its storage location, whereas most of the members of the union use the same location. Union contains many members of distinct types.
• Union can manage only one member at a time.
Here’s how it works with syntax.
union [union name] { member 1 definition; member 2 definition; ... member n definition; };
Union is declared using the “union” keyword and name of union. member 1, member 2, member n are individual members of union. The body part is terminated with a semicolon (;).
Sample Declaration of Union:
union student { int roll; char name[4]; int marks; }std;
Example of Union in C Programming
#include<stdio.h> union record { int x; float y; char ch; }; int main( ) { union record rec; rec.x = 12; rec.y = 20.2; rec.ch = 'a'; printf("%d\n", rec.x); printf("%f\n", rec.y); printf("%c\n", rec.ch); return 0; }
How Memory is Allocated in Union Members?
• Union Members that compose a union, all share the identical storage area within the memory of the computer.
• Every member in a structure is assigned its distinctive storage space.
• Hence unions utilized to observe memory.
• Unions are helpful for a program involving numerous members, in which values don’t assign to each of the members at any one time.
Advantages of union
Here, are benefits for using union:
• It occupies less memory in contrast to the structure.
• When you utilize union, only the final variable can directly get.
• Union operated when you’ve got to use the same memory location for a couple of data members.
• It allows you to hold data from one data member.
• Its efficient utilization of memory because it doesn’t require memory space to get its members; instead, it needs memory space to get its largest member only.
• same memory space can translate differently for various members of the union.
Disadvantages of union
Here, are drawbacks for using union:
• it’s possible to use one union member at one time.
• Each of the union variables cannot be initialized or utilized with varying values at one time.
• Union assigns one shared storage area for all its members.
Similarities between Structure and Union
• These two are user-defined data types used to store data of various types as one unit.
• Their members could be objects of any type, such as other structures and unions or arrays. A member may also include a bit field.
• Both structures and unions encourage only assignment = and sizeof operators. Both structures or unions in the assignment should have the same members and member types.
• A structure or a union can be passed by value to functions and returned by value by functions. The argument should have the identical type as the function parameter. A structure or union is passed by value much like a scalar variable as a corresponding parameter.
• ‘.’ operator used for accessing members.
Difference between Structure and Union in C
Following are the important difference between union and structure.
Structure | Union |
The keyword struct is used to specify a structure. | The keyword union is used to refer to a union. |
Every member within Structure is assigned a separate memory location. | In Union, a memory location shared with all of the data members. |
Altering the value of single data member won’t influence other data members of the Structure. | Altering the value of single data member will change other members value in Union. |
The total size of the Structure is the sum of the size of every data member. | The total size of the Union is the size of the most prominent data member. |
In Structure, multiple members may initialize simultaneously. | On the flip side, in the event of Union, only the first member can get initialize at a time. |
It mostly used for storing a variety of data types. | It mostly utilized for holding a few of many data types which are readily available. |
It occupies space for every single member written in inner parameters. | It occupies space to get a member with the maximum size composed in internal parameters. |
It’s possible to retrieve any member at one time. | It’s possible to get one member at one time from the Union. |
It supports flexible array. | It doesn’t support a flexible array. |