Enum in C
×


Enum in C

154

(Enumeration)Enums in C are a user-defined data type used to assign names to integral constants, making the code more readable and maintainable.

Enums allow you to define a set of named constants, which can represent a group of related values.

By using enums, you can create a group of related constants that represent a set of predefined values.

Way to define Enums in C:

enum Color {
    RED,
    GREEN,
    BLUE
};

Enum Declaration:

The enum keyword is used to declare an enumeration named Color.

Enum Values:

Inside the curly braces {}, we define the named constants: RED, GREEN, and BLUE.

Example:

// Program for Enums in C
#include<stdio.h>

// Enum Declaration
enum Weekdays {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};

int main() {
    // Enum Variable Declaration
    enum Weekdays today = WEDNESDAY;
    
    // Using Enum Constants
    if(today == WEDNESDAY) {
        printf("Today is Wednesday.\n");
    }
    
    return 0;
}

Output:

Today is Wednesday.

Enum Declaration:

We define an enumeration named Weekdays with constants representing each day of the week.

Enum Variable Declaration:

We declare an enum variable today and initialize it with WEDNESDAY.

Using Enum Constants:

We use enum constants in the if statement to check if today is WEDNESDAY and print a message accordingly.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments