Broad Network


Enumeration in the C Computer Language

Part 26 of the Complete C Course

Foreword: Enumeration in the C Computer Language

By: Chrysanthus Date Published: 22 Jun 2024

Introduction

An enumeration comprises a set of named integer constant values. Each distinct enumeration
constitutes a different enumerated type. An enumeration constant is of type int.

enum Basics
Consider the following program:

    #include <stdio.h>
    
    const int zero = 0; const int one = 1; const int two = 2;
    const int three = 3; const int four = 4;
    
    int main(int argc, char *argv[])
    {
        printf("%i ", zero); printf("%i ", one); printf("%i ", two);
        printf("%i ", three); printf("%i\n", four);

        return 0;
    }

The output is: 0 1 2 3 4 . Consider zero, one, two, three, four, as just identifiers that happen to be assigned their digits. All the objects are constants from 0 to 4. In this list of constants, the next constant is equivalent to 1 added to the previous constant. The constants begin from 0. Because of these characteristics, the program code could have been better written as:

    #include <stdio.h>
    
    enum {zero, one, two, three, four};
    
    int main(int argc, char *argv[])
    {
        printf("%i ", zero); printf("%i ", one); printf("%i ", two);
        printf("%i ", three); printf("%i\n", four);

        return 0;
    }

The same output is produced. In this code, zero, one, two, three, four are called enumerators, in the enumeration list. The whole statement with the reserved word, enum is called an enumeration. Do not forget that the value of each enumerator is a constant and cannot be changed, so far as the identifier is concerned. One variable can be made to refer to each of the constants in turn, as the following program shows:

    #include <stdio.h>
    
    enum Numbers {zero, one, two, three, four};
    
    int main(int argc, char *argv[])
    {
        enum Numbers number;
        
        number = zero; printf("%i ", number);
        number = one; printf("%i ", number);
        number = two; printf("%i ", number);
        number = three; printf("%i ", number);
        number = four; printf("%i\n", number);

        return 0;
    }

The output is the same: 0 1 2 3 4. In the declaration of the enumeration, there is a tag, Numbers. Here, Numbers is the type (or class) of enumeration. To create an enumeration object, begin with the reserved word, enum followed by a space and then the tag name (or type name), and then a space, then an identifier (name) for the enumeration object. That can be followed by a semicolon. In the above code, the printf() function call statement pairs, used the enumeration object, number, to refer to the different constants. number first referred to zero, then the same number referred to one; then two, then three and then four.

Non-Zero Start Integer
In the above examples, the value of the first integer is zero. Another number can be the start integer. To achieve this, an expression or value will have to be assigned to the first enumerator. Each next enumerator will have one added to it. The following program illustrates this.

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    
        enum numbers {twoDaysAgo = -2, Yesterday, today, tomorrow, afterTomorow, theDayAfter};
        
        printf("%i ", twoDaysAgo); printf("%i ", Yesterday); printf("%i ", today);
        printf("%i ", tomorrow); printf("%i ", afterTomorow); printf("%i\n", theDayAfter);

        return 0;
    }

The output should display the following integers: -2 -1 0 1 2 3 . There are six constant identifiers in the enumeration list. The first one was forced to be -2. So the second constant identifier has the value, -1; the third has the value 0; the fourth has the value 1; and the fifth has the value 2; and so on.

Remember: In the enumeration list, the difference between the values of the identifiers is 1, and ascending, everything being equal.

Modifying the Sequence
In the enumeration list, the difference between the values of the identifiers is 1, everything being equal. The range is determined by the value of the first identifier. If no value is assigned to any of the identifiers in the list, then the range begins from zero, adding 1 to the different identifier values, until the last identifier in the list, is reached.

If a value (integer) other than 0, is assigned to the first identifier, then the range begins from that number, adding 1 until the last identifier; on condition that no other identifier is assigned a value. That is how the enum construct works.

Now, to modify the sequence, an enumerator (identifier), other than the first in the list, can be assigned an integer, that is out of the expected range. The enumerators after this forced value, will have values increasing by 1, but continuing from that of the forced value. To achieve this, just assign the out-of-expected-range-value to an enumerator in the list. More than one out-of-expected-range-values can be forced into the list. The following program illustrates this:

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    
        enum numbers {twoDaysAgo = 10, Yesterday, today = 56, tomorrow, afterTomorow = 22, theDayAfter};
        
        printf("%i ", twoDaysAgo); printf("%i ", Yesterday); printf("%i ", today);
        printf("%i ", tomorrow); printf("%i ", afterTomorow); printf("%i\n", theDayAfter);

        return 0;
    }

The output is: 10 11 56 57 22 23 . The start value was forced to 10. The next value was 11. The third value was forced to 56. The next value was 57. The fifth value was forced to 22. The value after was 23. Remember: enum values accept only integers.  



Related Links

More Related Links

Cousins

BACK NEXT

Comments