Structure is a data type that stores different data types in the same memory location; the total memory size of the structure is the summation of memory sizes of all its members. In contrast, Union is a data type that stores different data types in the same memory location; the total memory size depends on the memory size of its largest elements. Meanwhile, Enum is a data type that store integral constants. That is the main difference between structure union and enum in C.
In programming, a variable can store a value of a single data type. Sometimes, it is necessary to store variables of different types as a single unit. Structure and union are two methods to store multiple variables of different types as a single variable. On the other hand, enum is a data type to declare a set of named constants. All these are user-defined data types.
Key Areas Covered
1. What is Structure
– Definition, Functionality
2. What is Union
– Definition, Functionality
3. What is Enum
– Definition, Functionality
4. What is the Difference Between Structure Union and Enum in C
– Comparison of Key Differences
Key Terms
C, Enum, Structure, Union
What is Structure
A structure is a single variable that can hold data of multiple types. It is a set of variables of dissimilar data types. An example of a structure is as follows.
According to the above program, the structure called complex contains two properties; real represents the real part of the complex number whereas img represents the imaginary part of the complex numbers. In the main program, line 10 declares two struct type variables called c1 and c2. Line 11 to line 14 give values to real and img of each c1 and c2. In line 15, the variable r stores the sum of real values of c1 and c2. Similarly, in line 16, the variable i stores the sum of img values of c1 and c2. Finally, the summation of complex numbers displays on the console.
What is Union
Union allows storing various data types in the same memory location. For example, an Employee can have properties such as name, salary, and, city. Instead of creating variables for each of them, it is possible to use a union. It compacts all different data types into a single unit.
The distance between two points p1 and p2 is as follows.
Distance = (p2.a – p1.a) 2 + (p2.b – p1.b) 2
According to the above program, Point is a union. It has two properties: a and b. In the main program, distance, t1 and t2 are variables of type float; p1 and p2 are type union. Line 12 to 15, gives values to the a and b of p1 and a and b of p2. t1 and t2 are variables stores the power values. In line 18, the variable distance stores the distance between point p1 and p2. Finally, the answer displays on the console.
Union and Structure are very similar, but they have a difference. In unions, the total memory required to store the union is the memory of the largest element in that union. For example, assume that a union has three properties: name, salary and id. The name takes 32 bytes, and the salary and id takes 4 bytes each. Largest is 32bytes, and the memory allocation for the union is 32 bytes.
What is Enum
Enum stands for enumeration. It is a user-defined data type that consists of integral constants. An example is as follows.
The week is an enum. By default, Sunday has the value 0, Monday has the value 1, Tuesday has the value 2, etc. In the main program, today is an enum of type week. It is assigned with Friday. The next day is Saturday, and it has constant 6. Therefore, the output “Day 6” will display on the console.
For example, assume that the programmer declared the enum as follows.
enum week { sunday=1, monday, tuesday, wednesday, thursday, friday, saturday };
Then, the main program will give the output as “Day 7”.
Difference Between Structure Union and Enum in C
Definition
Structure is a data type that stores different data types in the same memory location, and whose total memory size of the structure is the summation of memory sizes of all its members. In contrast, Union is a data type that stores different data types in the same memory location, and whose total memory size depends on the memory size of its largest elements. Enum is a data type in C language that represents a value type for declaring a set of named constants. These definitions outline the fundamental difference between structure union and enum in C.
Keyword
The keyword to declare a Structure is ‘struct’ while the keyword to declare a Union is ‘union’, and the keyword to declare an Enum is ‘enum’.
Usage
There is a difference between structure union and enum in C based on their usage as well. Both structure and union help to store data of different types as a single unit while enum helps to assign constants to a set of names to make the program easier to read, maintain and understand.
Conclusion
Structure is a data type that stores different data types in the same memory location; the total memory size of the structure is the summation of memory sizes of all its members. In contrast, Union is a data type that stores different data types in the same memory location; the total memory size depends on the memory size of its largest elements. Meanwhile, Enum is a data type that store integral constants. Thus, this is the main difference between structure union and enum in C.
Reference:
1. “C Programming Structure.” Python Strings (With Examples), Available here.
2. “C Programming Unions.” Python Strings (With Examples), Available here.
3. “C Programming Enumeration.” Python Strings (With Examples), Available here.
Leave a Reply