主页 Swift UNIX C Assembly Go Plan 9 Web MCU Research Non-Tech

What is enum in C language detailly

2023-04-19 | C | #Words: 1005

enum is abbreviation of enumerate. It is a type of constant in C language, called enumeration constant. enum provides a convenient way to associate values and names, and it is an alternative to #define.

enum can be regarded as a special array, which is a list of constants, but this constant just be integer.

So if there are only a few irrelevant and discontinuous constants, just define some constants directly is fine. If it is related or continuous, it is more efficient to use enum.

But there are three differences between enumerations and arrays:

  1. Enumeration uses keywords to locate element values, rather than indexs;
  2. The different enum can have the same value, but the names must be different;
  3. The default value of enum is 0, 1, 2..., and the default value of array is NULL or 0 according to the type.

If you don’t make sense, let me show some samples.

In “The C Programming Language”, the original text is the word “name”, rather than “keyword”.

This is the most common example on the Internet. Let’s take this as an example to start the detailed introduction:

enum months {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

The usage method given on the Internet is as follows (a bit complicated, so in reality, this method is not recommended, I will introduce a short method later):

int main()
{
    enum months mon;    /* Declare month is a value in `months`. Notice `mon` is a variable */
    mon = Jan;          /* The value of `mon` is equal to `Jan` */
    printf("%d\n",mon); /* Output the value of `mon` */
    return 0;
}

The output result here is 0. Because the value represented by the name is not defined, the default value 0, 1, 2... is used, and Jan is the first one, which is 0.

So if we want the value corresponding to its month?

You maybe think: It is an enumeration, so just equal them one by one. This is indeed, but the months here are consecutive integers, so you don’t need to equal them one by one, just set the first Jan=1, as follows:

enum months {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};

In this way, not only the output of Sep is 1, but also the output of Sep is 9, without the hassle of equalizing each one, and other correspond to its month.

You may be thinking: It’s must create a variable mon to get the value?

No. Because in many cases, obtaining the value of a constant through a variable is unnecessary. Only if you need use constant as initial, you maybe use variable to obtain constant value.

Not only mon is not necessary here, but even months is not necessary. Because enum is actually a collection of constants, and the content is distinguished by the name of the constant, not the name of enum. Here’s an example:

#include<stdio.h>

enum {Jan=1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec};
enum {Mon=1, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
    printf("%d, %d\n", Mon, Jan);
	return 0;
}

Output:

1, 1

So when to use the first method and when to use the second?

As mentioned at the beginning of the article, enum is a constant. Most people don’t simply make a variable equal to a constant, then through outputing the variable to output the constant value. They just output the constant directly.

So in most cases, you can just use the second, because the names will not be repeated. The first method is for some special cases. For example, if you want the function return value to have 4 situations (the actual situation may be more), the integer value of the order represented by each situation is 0, 1, 2, 3, as follows:

enum state {Standard, Error, Position_1, Position_2}

At this time the code can be written as (a lot simplified here just to represent this situation):

int test() {
    enum state ret
    if (...) {
        ret = Standard
    } else if (...) {
        ret = Error
    } else if (...) {
        ret = Position_1
    } else if (...) {
        ret = Position_2
    }
    return ret
}

It’s much easier.

I hope these will help someone in need~