Loading...
Loading...
00:00:00

What Is Enumerators In C?

In C language, Enumerators or enum is a special data type which allow to create a list in sequences order. In Enumerator we can write , comma seperated variables name that is internaly treated as int in increasing order and starts with 0 . we can specify the number for variables but default value starts from 0. For create a Enumerator we have to use enum keyword. In simple way, use number as a word;

Syntax of Enumerator:

enum enumerator_name
{    // data item
    item1, item2, item3, item4 , itemN
}

Enumerators Example

Create Months as Enumerator for getting position number for their orders.

#include <stdio.h>
#include <stdlib.h>

enum Months
{
    // assign integer number for january
    january = 1, february, march, april, may, june, july, august, september, october, november, december 
};

int main(int argc, char const *argv[])
{
    printf("The position of january is %d\n",january);  // get the integer number associate with
    printf("The position of february is %d\n",february);  // get the integer number associate with
    printf("The position of october is %d\n",october);  // get the integer number associate with
    return 0;
}

Create A Custom Boolean Data Type Using Enumerator (Enum) In C

Example create a boolean value using Enumerator.

#include <stdio.h>
#include <stdlib.h>

enum boolean
{
    // create a boolean
    false,
    true
};
// function for check positive number it's return type is boolean which is enum
enum boolean Positive_number(int x)
{
    if (x >= 0)
        return true;
    return false;
}

int main(int argc, char const *argv[])
{
    if (Positive_number(-50))
        printf("the number is positive \n");
    else
        printf("The number is not positive\n");
    printf("The position of true is %d in numerator\n", true);   // get the integer number associate with
    printf("The position of false is %d in numerator\n", false); // get the integer number associate with
    return 0;
}