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!
c enums standards
Bryan Henry
source share