开发者

Why isn't an enum checked by the C compiler?

开发者 https://www.devze.com 2023-04-04 21:52 出处:网络
The following text is an excerpt from C Programming Language, 2nd Edition, written by the creator of the C language (so I presume it is correct):

The following text is an excerpt from C Programming Language, 2nd Edition, written by the creator of the C language (so I presume it is correct):

Although variables of enum types may be dec开发者_如何学Pythonlared, compilers need not check that what you store in such a variable is a valid value for the enumeration.

I have some doubts:

  1. For what cases in the C language doesn't the compiler check the value of an enum?
  2. enum constants are not checked for some reason. Why not? What are the reasons?
  3. Since enum is not checked by the compiler, is using enum error-prone? Please explain.


  1. An enumeration is like a fancy integer, and it's better than defining a whole load of constants or preprocessor macros as names for the constant values you want to store, because a compiler (or editor) can check that you're using the right names and values to go with the right type. On the other hand, being just an int, there's nothing stopping you putting in a value you didn't make a name for, which is occasionally useful.

  2. They can't be checked in every case. What if you add two numbers together to get the value that's going to be put in the enum-typed variable? It could be any value, generated at runtime, so it can't be checked (without a lot of overhead, at least).

  3. Everything in C is unsafe; there's practically no feature which the compiler can totally prevent you from abusing. enums are safe because they are effective at preventing programmer error and confusion, not because they stop you doing something stupid.


You can do a enum like

enum status {
    ST_READY = 1 << 0, /*  1 */
    ST_WAIT  = 1 << 1, /*  2 */
    ST_ERROR = 1 << 2, /*  4 */
    ST_HALT  = 1 << 3, /*  8 */
    ST_ETC   = 1 << 4, /* 16 */
};

Then define an object of that type

enum status status;

and set it to the bitwise OR of some 'simple' statuses

status = ST_WAIT | ST_ERROR; /* recoverable error */

Note that the value ST_WAIT | ST_ERROR is 6 and that that value is not part of the enum.


To answer your questions:

  1. C compiler lets the programmer shoot himself in the foot.
  2. C compiler lets the programmer shoot himself in the foot.
  3. C compiler lets the programmer shoot himself in the foot.


  1. At what all cases in C language doesn't the compiler check the value of an enum. [edited]

When you assign to it. Assignment from bare integers is allowed, so you can do:

enum E { A, B } x;
x = 10000;

without compiler error. Also, switches in enums are not check for exhaustiveness.

2)Why enum constants are not checked, for some reasons? What are those reasons?

People like to put integers in them. eg.

enum E { END_OF_EVERYTHING = 5 };

where 0-4 are meant as ordinary values, and 5 is kind of special.

  1. Since enum is not checked by the compiler, using enum is error prone?

Yes. Since enums only have values of the least number of bits that can take all enum values, you can get strange results:

enum E { A = 1, B = -1 };

This enum only contains 2 bits of data (values -2, -1, 0, 1). If you assign 10000 into it, strange things may happen (actually seen).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号