Structure and union are both user defined data types of C programming language. User defined data type is a data type that the user i.e. the programmer will decide what type it will be. Sometimes we need to do some complex programs when using basic data types is difficult. In these cases we can make things much easier by using these structures and unions. Now we will learn about the structure and usage of these two data types –
Structure
The struct keyword is used to define the structure. Then the name of the structure i.e. the name of our desired variable. Now we will create a structure and learn about its various parts –
/*
Author: Shahinur
Description: This program is to demonstrate the structure in C
*/
#include <stdio.h>
#include <string.h>
struct Student{
unsigned long int roll;
char name[20];
char dept[10];
};
int main() {
struct Student s1,s2;
s1.roll=100120;
strcpy(s1.name,"Shahinur");
strcpy(s1.dept,"CSE");
printf("Roll:%d\nName:%s\nDepartment:%s\n",s1.roll,s1.name,s1.dept);
printf("The memory size is: %d",sizeof(s1));
return 0;
}
In line 9 we have declared a structure. After the struct keyword we give a name Student, this is the name of our defined variable. Then we arrange it with basic data types as needed. The structure is terminated by a semicolon at the end of the bracket.
In line 16 we have declared a variable s1 and s2 of structure type called Student. For the sake of brevity of code we have only used the s1 variable tie here.
The dot(.) operator is used to access a specific element of the structure. In line 18, we access the roll element of s1 with s1.roll and assign a value to it just like assigning to a normal variable. The rest are the same. Note here, we have used the elements of the s1 variable. This will not affect s2. s2 must be accessed using the dot operator in the same way.
Here is my output of the above code:
Roll:100120
Name:Shahinur
Department:CSE
The memory size is: 40
Note here that our s1 variable occupies 40 bytes of memory. Let’s do some calculations.
As we know, normal int occupies 2 bytes of memory. But we have used unsigned long int here, and this unsigned long int occupies 8 bytes of memory. On the other hand we know that char type occupies 1 byte of memory space. Therefore memory size = 8+20+10 = 38 bytes. It may have occurred to me that our program is showing memory size 40, then why is it 38? This is because the remaining 2 bytes are used for padding.
Union
Unions and structures do similar things. Only the memory calculation is different. The memory size of the structure is equal to or greater than the sum of the memory sizes of the basic datatypes used. But the union is a little different. The union’s memory size is equal to or greater than the size of the largest data type used in that union. If we replace the above code with union code then we get –
#include <stdio.h>
#include <string.h>
union Student{
unsigned long int roll;
char name[20];
char dept[10];
};
int main() {
union Student s1,s2;
s1.roll=100120;
strcpy(s1.name,"Shahinur");
strcpy(s1.dept,"CSE");
printf("Roll:%d\nName:%s\nDepartment:%s\n",s1.roll,s1.name,s1.dept);
printf("The memory size is: %d",sizeof(s1));
return 0;
}
Notice here only struct is replaced with union. My memory size here is 24. That is, the maximum size is 24 by adding padding to name.
Hope everyone understands. However, if you find anything complicated, please comment.
Thank you