Hello Everyone,
Today we are going to talk a bit about the difference between structure and union as in C programming language.
We are going to provide a coded approach so that you could see the difference practically as the theoretic differences you can read from anywhere on the internet and your text books.
Here comes the code first for the same…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include<stdio.h> // Define the student structure struct student { int roll; char name[10]; }; // Define the Employee union union employee { int empid; char name[10]; }; // Declare both structure and union as types typedef union employee emp; typedef struct student student; // Main function int main() { // declare variable of type student structure student s; // declare two variables of type employee union emp e1, e2; // print the sizes of each printf("\n size of int %d", sizeof(int)); printf("\n size of char %d", sizeof(char)); printf("\n size of student structure variable: %d", sizeof(s)); printf("\n size of employee union variable: %d", sizeof(e1)); // Gather data from the user // Get values for student printf("\n\n Enter the roll number of student (integer): "); scanf("%d", &s.roll); printf(" Enter the name of student: "); scanf("%s", s.name); // get values for first employee e1 (id then name) printf(" Enter the id of first employee (integer): "); scanf("%d", &e1.empid); printf(" Enter the name of the first employee: "); scanf("%s", e1.name); // Get values for second employee but in REVERSE order (name then id) printf(" Enter the name of the second employee: "); scanf("%s", e2.name); printf(" Enter the id of second employee (integer): "); scanf("%d", &e2.empid); // Access and print the values // Access and print the student structure printf("\n Accessing the Student Structure"); printf("\n The roll number of student is: %d", s.roll); printf("\n The Name of student is: %s", s.name); // Access and print the first Employee union printf("\n\n Accessing the first Employee Union"); printf("\n The ID of employee is: %d", e1.empid); printf("\n The name of employee is: %s", e1.name); // Access and print the second Employee union printf("\n\n Accessing the second Employee Union"); printf("\n The ID of employee is: %d", e2.empid); printf("\n The name of employee is: %s", e2.name); /* * Observe that the order of taking input for the two employee unions were reversed. * Write your observation as to what is the effect of this input order reversal * on the values being stored in the memory? * Will such a reversal affect the values being stored in student structure as well? */ printf("\n"); return 0; } |
As defined in the code above, we are declaring a student structure first and then an employee union next.
As we know, structure allocates memory for all individual members in it while the union allocates memory only for the largest data unit present in it.
The above difference of memory allocation size is displayed by printing the size of one variable each for struct and union.
Next we display the difference in storage and accessing the members of the two constructs. First we take input for student structure and then for two employee unions. However for the union variables we take the inputs in opposite order. This shows that only one member is accessible at a time. The last input value overrides the previous input value in the union.
A sample output for the program is displayed below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
$ ./a size of int 4 size of char 1 size of student structure variable: 16 size of employee union variable: 12 Enter the roll number of student (integer): 21 Enter the name of student: Anupam Enter the id of first employee (integer): 32123 Enter the name of the first employee: Payam Enter the name of the second employee: Anupam Enter the id of second employee (integer): 123123 Accessing the Student Structure The roll number of student is: 21 The Name of student is: Anupam Accessing the first Employee Union The ID of employee is: 1635344720 The name of employee is: Payam Accessing the second Employee Union The ID of employee is: 123123 The name of employee is: ▒▒ |