C – Enums

In this lesson, let us learn what are Enums in C programming. We will cover the following concepts:

  • What are Enums
  • Create an Enum
  • Create an Enum variable
  • Assign a value to an Enum variable
  • Example of Enum

What are Enums

We studied some types in C. However, there is another type in C programming called Enums. Enums, stand for enumerations i.e., a group of constants. Use enums to name constants.

In C, we use the enum keyword to create an enum. Let us see the syntax:

Above, we have the enum name after the enum keyword. Then comes the enum items separated by comma.

Create an Enum

Let us now see how to create an enum. We will consider Directions and enum items as NORTH, SOUTH, etc:

Create an Enum Variable

Create a variable of the Enum if you want to access it. Let us create a variable of the above enum:

Assign a value to the enum variable

On assigning a value to the enum, the  resultant value will display an increment based on the enum items i.e.:

Enums in C

Enum – Example

Let us now see an example to create an Enum in C. We will also assign value to the enum items, and display:

Output


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

C - Variable Scope
C - Pointers
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading