A variable called the same as an enumerator

What is a specific behavior for something like the following?

#include <stdio.h> typedef enum { ENUM_VAL_1 = 1, ENUM_VAL_2 = 2 } TEST_ENUM; int main() { TEST_ENUM testVar1 = ENUM_VAL_1; TEST_ENUM ENUM_VAL_1 = ENUM_VAL_1; TEST_ENUM testVar2 = ENUM_VAL_1; printf("ENUM_VAL_1 = %u\n",ENUM_VAL_1); printf("testVar1 = %u\n",testVar1); printf("testVar2 = %u\n",testVar2); return 0; } 

From my testing with both GCC and MSVC compilers, the behavior of this is that testVar1 will be set to the value of the enum "ENUM_VAL_1" or 1. However, the following statement will try to set the ENUM_VAL_1 variable to its own value, which, of course, is the current uninitialized and therefore garbage, instead of setting the ENUM_VAL_1 variable equal to the value of the ENUM_VAL_1 enumeration. Then, of course, testVar2 will also get the same garbage value as the ENUM_VAL_1 variable.

What is the specific behavior of this according to C standards, or is this behavior undefined? Regardless of whether this is defined or not, I assume that this type of example is bad practice, at least because of ambiguity.

Thanks!

+7
c enums standards
source share
2 answers

In accordance with standard C (6.2.1 Identifier areas)

  1. ... If the identifier denotes two different objects in the same namespace, the scope may overlap. If so, the volume of one object (internal volume) will be completely closed in front of the area of ​​another (external coverage). Within the inner region, an identifier denotes an object declared in the inner region; the declared object in the outer space is hidden (and not visible) inside the inner sphere.

and

7 Structure, join, and enumeration tags have a scope that begins only after the tag appears in the type specifier that declares the tag. Each enumeration constant has an area that begins immediately after the appearance of its defining enumerator in the list of enumerators. Any other identifier has a scope that begins immediately after its declarator

So in this ad

 TEST_ENUM ENUM_VAL_1 = ENUM_VAL_1; 

declarator ENUM_VAL_1 is considered completed before the = sign. Therefore, it hides the counter.

In fact, it is initialized by itself and has an indefinite value.

The same is true for C ++ (3.3.2 declaration point)

1 The declaration point for the name immediately after its full declarator (section 8) and before its initializer (if any), except as noted below. [Example:

 int x = 12; { int x = x; } 

Here the second x is initialized with its own (undefined) value. -end example]

+6
source share

I expected the line TEST_ENUM ENUM_VAL_1 = ENUM_VAL_1; will not compile, but it will happen. I changed the assigned value to ENUM_VAL_2 and then print ENUM_VAL_1 = 2 , testVar1 = 1 and testVar2 = 2 , so ENUM_VAL_1 is a local variable.

This is actually a routine problem; this means that declaring the variable in main() overshadows the declaration outside - and if the typedef is within main() , the code will not compile. Add -Wshadow to your compilation options to see the shading. After setting testVar1 , ENUM_VAL_1 means a local variable, not an enum constant. Initializing a variable by itself does not initialize the variable; it copies undefined garbage to value.

+3
source share

All Articles