Why is the variable not checked by the C compiler?

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

Although enumeration type variables can be declared, compilers do not need to verify that what you store in such a variable is a valid value for the enumeration.

I have some doubts:

  • In what cases in C does the compiler not check the enum value? Constants
  • enum not checked for any reason. Why not? What are the reasons?
  • Since enum not checked by the compiler, uses enum error prone? Please explain.
+7
source share
3 answers
  • Enumeration is like a fantastic integer, and it's better than defining an entire load of constants or preprocessor macros as constant value names that you want to keep, since the compiler (or editor) can check re using the correct names and values ​​for the correct input. On the other hand, being just an int, nothing prevents you from inserting a value for which you did not create a name, which is sometimes useful.

  • They cannot be checked in every case. What if you add two numbers together to get a value that will be placed in an enumerated variable? It can be any value generated at runtime, so it cannot be checked (without a lot of overhead, at least).

  • Everything in C is unsafe; there is practically no possibility that the compiler can completely stop you from abusing it. enumerations are safe because they are effective for preventing programmer errors and confusion, and not because they stop doing something stupid.

+8
source

You can do the listing as

 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 this type

 enum status status; 

and set it bitwise OR to some "simple" statuses

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

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


To answer your questions:

  • The C compiler allows the programmer to shoot himself in the foot.
  • The C compiler allows the programmer to shoot himself in the foot.
  • The C compiler allows the programmer to shoot himself in the foot.
+6
source

1) The fact is that in all cases in the C language, the compiler does not check the value as enum.

I can’t understand what you are saying to your tribe.

1) In all cases in the C language, the compiler does not check the value of the enumeration. [Edit]

When you appoint him. Assignment from bare integers is allowed, so you can do:

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

without compiler error. In addition, switch es in enumerations do not check comprehensive information.

2) Why are enumeration constants not checked for some reason? What are these reasons?

People like to insert integers into them. eg.

 enum E { END_OF_EVERYTHING = 5 }; 

where 0-4 mean like ordinary values, and 5 is special.

3) Since enum is not checked by the compiler, is using enum error prone?

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

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

This enumeration contains only 2 bits of data (values ​​-2, -1, 0, 1). If you enter 10,000 into it, strange things (actually seen) can happen.

+1
source

All Articles