-
Notifications
You must be signed in to change notification settings - Fork 2
/
Enum vs Union vs Structure.txt
42 lines (38 loc) · 2.41 KB
/
Enum vs Union vs Structure.txt
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
In C programming, `enum`, `union`, and `structure` are used to define custom data types that group related variables. While all three constructs are used for similar purposes, they have different characteristics and usage scenarios.
1. Enumerations (enum):
An enumeration is a user-defined data type that assigns symbolic names to a set of related integer values. The primary purpose of using an enum is to make the code more readable and maintainable. For example:
```
enum week_days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
enum week_days today = Monday;
```
In the above example, `week_days` is an enum type that defines the days of the week. The names `Monday`, `Tuesday`, and so on, are symbolic constants that represent the values 0, 1, and so on, respectively. The variable `today` is declared as an enum type and initialized to the value `Monday`.
2. Unions:
A union is a user-defined data type that allows the storage of different data types in the same memory location. The primary purpose of using a union is to save memory and facilitate type conversion. For example:
```
union number {
int integer;
float floating_point;
char character;
};
union number num;
num.integer = 10;
printf("%d\n", num.integer);
num.floating_point = 3.14;
printf("%f\n", num.floating_point);
```
In the above example, `number` is a union type that can store an integer, floating-point, or character value. The variable `num` is declared as a union type and can be assigned values of different types. When a new value is assigned, the previous value is overwritten.
3. Structures:
A structure is a user-defined data type that groups related variables of different data types. The primary purpose of using a structure is to organize data and make it easier to manipulate. For example:
```
struct student {
char name[50];
int age;
float gpa;
};
struct student s;
strcpy(s.name, "John");
s.age = 20;
s.gpa = 3.5;
```
In the above example, `student` is a structure type that defines the attributes of a student, such as name, age, and GPA. The variable `s` is declared as a structure type and initialized with specific values.
The main difference between enums, unions, and structures is their purpose and usage scenarios. Enumerations are used to define a set of related integer values, unions are used to store different data types in the same memory location, and structures are used to group related variables of different data types.