Enumeration Scope

As the others already said enumeration constants must be unique in the actual scope where they are defined. But with them as with other identifiers it is allowed to redefine them in another scope. Eg.

enum EnumA
{
  stuffA = 0
};

void func(void) {
   enum enumAA
   {
     stuffA = 1
   };
   // do something
}

would be fine. But such redefinitions in different scopes are often frowned upon and should be well documented, otherwise you will quickly loose yourself and others.


The enumeration constants are in the global name space (more precisely, the ordinary identifiers name space, contrasted with the labels, tags, and structure/union member namespaces), so you get a compilation error on the second stuffA.

You cannot use two different values for the same enumeration name (nor the same value specified twice) in a single translation unit.


enums don't introduce new scope.

In your example, the second enum wouldn't compile due to the stuffA name clash.

To avoid name clashes, it is a common practice to give the elements of an enum a common prefix. Different prefixes would be used for different enums:

enum EnumA
{
  EA_stuffA = 0
};
enum EnumAA
{
  EAA_stuffA = 1
};

Tags:

C

Enums