Unions in C are user-defined data type that allows
storing different types of data in the same memory
location. They enable efficient memory utilization by sharing the same memory space for different data types. The syntax to declare a union is: union union_name { member1; member2; ... }; Example: union employee { int empId; float salary; char name[20]; }; Union members can be accessed using the dot (.) operator. Example: union employee emp; emp.empId = 123; emp.salary = 5000.0; Thesize of a union is determined by the largest member size. Example: ◦ If a union has a member of type int (4 bytes) and another member of type float (4 bytes), the size of the union will be 4 bytes. Unionscan be initialized similar to structures. Example: union employee emp = { .empId = 123, .salary = 5000.0 }; Unions share the same memory location for all its members. Accessing a member other than the last one written can result in undefined behavior. Unions are useful when we want to interpret the same memory location as different data types. Example: union value { int intValue; float floatValue; }; Unions are often used in scenarios where different types of data need to be stored in the same memory space, such as parsers, network programming, and hardware manipulation. Unions can be useful in many situations where we want to use the same memory for two or more members. For example, suppose we want to implement a binary tree data structure where each leaf node has a double data value, while each internal node has pointers to two children, but no data. If we declare this as: struct NODE { struct NODE* left; struct NODE* right; double data; }; Memory efficiency: Unions save memory by sharing memory space for different types of data. Versatile data representation: Unions are useful when different data types need to be stored in the same memory location. Interpretation flexibility: Unions allow interpreting the same memory location as different data types, providing versatility in data retrieval. Limitedsimultaneous use: Only one member of a union can be used at a time, and accessing a member other than the last one written can lead to undefined behavior. Enumeration in C is a user-defined data type that consists of a set of named constants. It allows us to define a list of related named values. The syntax to declare an enumeration is: enum enum_name { value1, value2, ... }; Example: enum days { MON, TUE, WED, THU, FRI, SAT, SUN }; Enumeration constants are assigned integer values by default, starting from 0. Example: In enum days, MON is assigned 0, TUE is assigned 1, and so on. You can explicitly assign values to enumeration constants. Example: enum months { JAN = 1, FEB = 2, MAR = 3, APR = 4 }; Enumeration constants can be accessed using the dot (.) operator. Example: enum days today = MON; Enumeration constants are often used in switch statements for better readability. Example: enum days today = MON; switch(today) { case MON: // Code for Monday break; case TUE: // Code for Tuesday break; // Rest of the cases } The enumeration type is referred to as the "enum" and its individual values are called "enumerators." The size of an enumeration constant is determined by the underlying integer type. By default, it is int, but it can be explicitly specified using a colon (:) followed by the desired type. Example: enum sizes : short { SMALL, MEDIUM, LARGE }; Improved code clarity: Enums provide meaningful names to represent related values, making the code more understandable. Easy maintenance: If a value needs to be added or removed, it can be done by modifying the enum declaration only. Compiler checks: The compiler can perform type checking, ensuring that only valid enum values are used. Limited values: Enums are restricted to a fixed set of values, and they cannot represent arbitrary values or complex data structures.