Why do two different enumeration constants have the same integer value?
Since this is explicitly permitted by the standard project N1265 C99 in 6.7.2.2/3 "Listing Qualifiers":
Using counters with = can lead to enumeration constants with values that duplicate other values in the same enumeration.
How will the system differentiate MON and TUE internally?
I think this is not possible because they are compile time constants (6.6 / 6 "Constant Expressions"). As a result, they:
cannot be changed to differ after compilation
don't have an address to tell them apart: The storage location of the enum value in C
Compile-time constants do not need any address, because addresses are useless for things that you cannot change.
GCC simply replaces the use of enumeration members with immediate values in the assembly at compile time. Consider:
#include <stdio.h> enum E { E0 = 0x1234, E1 = 0x1234 }; int i = 0x5678; int main() { printf("%d\n", E0); printf("%d\n", E1); printf("%d\n", i); return 0; }
Compile and decompile with GCC 4.8 x86_64:
gcc -c -g -O0 -std=c89 main.c objdump -Sr main.o
The output contains:
printf("%d\n", E0); 4: be 34 12 00 00 mov $0x1234,%esi ... printf("%d\n", E1); 18: be 34 12 00 00 mov $0x1234,%esi ... printf("%d\n", i); 2c: 8b 05 00 00 00 00 mov 0x0(%rip),%eax
So we see that:
- enumeration members are used as direct
$0x1234 , so it’s impossible to know where they came from - the variable
i , however, comes from memory 0x0(%rip) (for moving), so the two variables can be differentiated by address
Ciro Santilli 华 涌 低端 人口 六四 事件 法轮功 Jun 18 '15 at 13:15 2015-06-18 13:15
source share