Hi group,
I was just a bit surprised about the behavior of my compiler and traced
a bug down to the fact that enum symbols had duplicate integer values.
Now I'm unsure if that is permissible by the standard (I guess it is,
but it still surprises me). Consider this code:
#include <stdio.h>
enum foo {
MOO,
KOO,
BAR,
ZOO = 0,
};
int main() {
printf("%d\n", MOO);
printf("%d\n", KOO);
printf("%d\n", BAR);
printf("%d\n", ZOO);
return 0;
}
The output of which is on my system using gcc:
0
1
2
0
When I would have thought it'd output:
1
2
3
0
So the reuse of integer values that have explicitly been set by the
programmer is permissible by the C standard?
When I rearrange the values, it works as expected (the compiler only
ever chooses automatic values that are greater than all the previously
parsed values). Is this also guaranteed by the C standard?
Besr regards,
Johannes
--
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$(E-Mail Removed)>
I was just a bit surprised about the behavior of my compiler and traced
a bug down to the fact that enum symbols had duplicate integer values.
Now I'm unsure if that is permissible by the standard (I guess it is,
but it still surprises me). Consider this code:
#include <stdio.h>
enum foo {
MOO,
KOO,
BAR,
ZOO = 0,
};
int main() {
printf("%d\n", MOO);
printf("%d\n", KOO);
printf("%d\n", BAR);
printf("%d\n", ZOO);
return 0;
}
The output of which is on my system using gcc:
0
1
2
0
When I would have thought it'd output:
1
2
3
0
So the reuse of integer values that have explicitly been set by the
programmer is permissible by the C standard?
When I rearrange the values, it works as expected (the compiler only
ever chooses automatic values that are greater than all the previously
parsed values). Is this also guaranteed by the C standard?
Besr regards,
Johannes
--
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
- Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$(E-Mail Removed)>
Aucun commentaire:
Enregistrer un commentaire