Union and Enum in C

Level : Advanced
Mentor: Shailendra Chauhan
Duration : 00:01:00

What is a Union in C?

A union is a variable that can hold objects of various types and sizes, with the compiler taking track of the size and alignment needs. It allows you to modify several types of data in a single storage location without including any machine-dependent information in the program.​

Declaration of a Union

In this part, we just declare the union's template, which includes the names of the members and their data types, as well as the union's name. The declaration allocates no memory to the union.

Different Ways to Define a Union Variable

To use union members, we must first define a union-type variable. There are two approaches to defining a union variable.

  1. With Union Declaration
  2. After Union Declaration

Size of Union

The union's size will always be the same as that of the array's largest member. All of the smaller elements can store data in the same place without exceeding their capacity.

What is an Enumeration/enum in C?

An enumeration (enum) is a user-defined type (similar to structure) that represents a collection of constants. An enum is a user-defined data type that allows a variable to take only one of the specified values. Assigning names to integral constants improves program readability and maintainability.​

When enum is used in C?

Enums in C are used when a variable has a defined set of possible values that will never change, offering a simple and easy way to deal with such constants. This improves code readability and reduces error rates.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this