What is the difference between struct union and enum in C

Structure is a data type that stores different types of data in the same memory location; The total memory size of the structure is the sum of the memory sizes of all its members. In contrast, Union is a data type that stores different types of data in the same memory location; The total size of memory depends on the size of the memory of its largest elements. Meanwhile, Enum is a data type that stores integral constants.That is the main difference between struct binding and enum in C.

In programming, a variable can store a value of only one 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 for declaring a set of named constants. These are all user-defined data types.

Key Areas Covered What is the difference between struct union and enum in C

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 differences key code

Key terms

C, Enum, Structure, Union

What is the structure

A structure is a single variable that can contain data of various types. It is a set of variables of different data types.

Figure 1: Structure in C

According to the above program, the structure called complex contains two properties; real represents the real part of the complex number, while img represents the imaginary part of complex numbers. In the main program, line 10 declares two struct type variables named c1 and c2. Line 11 to line 14 give real and img values ​​of each c1 and c2. On line 15, the variable r stores the sum of the real values ​​of c1 and c2. Similarly, on line 16, the variable i stores the sum of the img values ​​of c1 and c2. Finally, the sum of complex numbers is displayed on the console.

what is union

Union allows several types of data to be stored in the same memory location. For example, an employee may 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 the different types of data into a single unit.

Figure 2: Junction at C

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 syndicate. It has two properties: a and b. In the main program, the distance, t1 and t2 are variables of type float; p1 and p2 are of type union. Line 12 to 15 gives values ​​to a and b of p1 and a and b of p2. t1 and t2 are variables that store the power values. In line 18, the variable distance stores the distance between the point p1 and p2. Finally, the response is displayed on the console.

The binding and the structure are very similar, but they have a difference. For joins, the total memory required to store the join is the memory of the largest element in that join. For example, suppose a union has three properties: name, salary, and id. The name takes 32 bytes, and the salary and id take 4 bytes each. The largest is 32 bytes, and the memory allocation for the union is 32 bytes.

what is enum What is the difference between struct union and enum in C

Enum means enumeration. It is a user-defined data type consisting of integral constants. An example is the following.

Figure 3: Enum in C

The week is an enumeration. By default, Sunday is set to 0, Monday is set to 1, Tuesday is set to 2, and so on. In the main program, today is a week-type enumeration. It is assigned with Friday. The next day is Saturday and has a constant of 6. Therefore, the output “Day 6” will be displayed on the console.

For example, suppose the programmer declared the enumeration as follows.

enum week sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday;

Then the main program will output as “Day 7”.

Difference between Union Structure and Enum in C

Definition

Structure is a data type that stores different types of data in the same memory location and whose total memory size of the structure is the sum of the memory sizes of all its members. In contrast, Union is a data type that stores different types of data 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 the C language that represents a value type for declaring a set of named constants. These definitions describe the fundamental difference between structure binding and enumeration in C.

Keyword What is the difference between struct union and enum in C

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’.

Use

There is a difference between struct binding and enumeration in C depending on their usage as well. Both struct and union help to store data of different types as a single unit while enumeration 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 types of data in the same memory location; The total memory size of the structure is the sum of the memory sizes of all its members. In contrast, Union is a data type that stores different types of data in the same memory location; The total size of memory depends on the size of the memory of its largest elements. Meanwhile, Enum is a data type that stores integral constants. Thus, this is the main difference between struct binding and enumeration 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.

Ads by optAd360

The difference between objects and similar terms. Comparisons of things, technology, cars, terms, people and everything that exists in this world.

Leave a Reply

Your email address will not be published.

Back to top button