13
SepDifference Between Structure and Union in C Language
Structure and Union in C Programming
Do you know C language can store elements of the same as well as different data types? Arrays store the same data type elements while structure and union store different data type elements. Structure and Union are user-defined data types in C. Both store elements of different data types of varying sizes.
In this C tutorial, we'll study the difference between structure and union with examples in C programming.
Read More: Top 50 Mostly Asked C Interview Questions and Answers
What is a Structure in C?
The structure is a user-defined data type declared with a "struct" keyword. It combines different datatypes into a single datatype under one name. All the elements of a structure are stored at contiguous memory locations. The total memory size of a structure is the sum of the sizes of all its elements. You can access the structure elements using the dot (.) operator.
Syntax of Structure in C
struct structure_name
{
type member1;
type member2;
type member3;
};structure_variables;
The structure variables are the structure objects for accessing the structure members.
What is a Union in C?
Union is a user-defined data type declared with a "union" keyword. It is a collection of elements of different data types. All the elements are stored at the same memory location. Only one member can hold a value at a time in a union. The size of the memory depends on the largest member of the union.
Syntax of Union in C
union union_name {
type member1;
type member2;
type member3;
};union_variables;
Difference Between Structure and Union in C Programming
Parameters | Structure | Union |
Keyword | A structure is declared using the "struct" keyword | A union is declared using the "union" keyword |
Memory | All the structure members have a unique memory location. | All the members share the same memory location. |
Value | Stores distinct values for all the members | Stores same values for all the members |
Size | The sum of the sizes of all the members of a structure | The size of the largest member of the union |
Initialization of member variables | Multiple members can be initialized at the same time | Only one member can be initialized at a time |
Accessing Members | You can access individual members at a time | You can access only one member at a time |
Data types | It's used for storing various data types | It's used to store one of the many available data types. |
Altering values | Change in the value of any member does not affect other member's value | Change in the value of one member will affect the value of other members in the union. |
Difference Between Structure and Union with Example
#include <stdio.h>
#include <string.h>
// Define a structure
struct ExampleStruct {
int intVar;
float floatVar;
char str[20];
};
// Define a union
union ExampleUnion {
int intVar;
float floatVar;
char str[20];
};
int main() {
// Declare and initialize a structure
struct ExampleStruct s;
s.intVar = 42;
s.floatVar = 3.14;
strcpy(s.str, "Hello, World!");
// Declare and initialize a union
union ExampleUnion u;
u.intVar = 42;
u.floatVar = 3.14;
strcpy(u.str, "Hello, World!");
// Print structure values
printf("Structure values:\n");
printf("intVar: %d\n", s.intVar);
printf("floatVar: %.2f\n", s.floatVar);
printf("str: %s\n", s.str);
// Print union values
printf("\nUnion values (after setting str):\n");
printf("intVar: %d\n", u.intVar);
printf("floatVar: %.2f\n", u.floatVar);
printf("str: %s\n", u.str);
return 0;
}
Output
Structure values:
intVar: 42
floatVar: 3.14
str: Hello, World!
Union values (after setting str):
intVar: 1819043144
floatVar: 1143139122437582505939828736.00
str: Hello, World!
In the union declared above, after setting the string value, the intVar and floatVar contain garbage values or may be undefined because they share the same memory location with str.
Related Differences: