Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an enumeration constant and an enumerator?

In C17's final draft N2176,

in the 1st paragraph of 6.2.1 it says

member of an enumeration is called an enumeration constant.

in the 7th paragraph 6.2.1 it says

Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list.

Now consider this example code:

enum days
{
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
};
  1. In the above code, the symbols MONDAY, TUESDAY, etc are the enumerators and the values 0, 1, 2, 3, 4, and 5 are enumeration constants, right?
  2. If yes, then what does the scope of an enumeration constant mean? (I interpret the phrase scope of an enumeration constant as the scope of an integer constant in the enumeration list, say 0, which is not making sense. The phrase scope of an enumerator makes more sense to me).

Please explain the distinction between an enumeration constant and an enumerator.

like image 779
Cinverse Avatar asked Sep 01 '25 03:09

Cinverse


1 Answers

Enumeration constant is the name part of the enumerator:

6.7.2.2 Enumeration specifiers

Syntax

  • enum-specifier:
    • enum identifieropt { enumerator-list }
    • enum identifieropt { enumerator-list , }
    • enum identifier
  • enumerator-list:
    • enumerator
    • enumerator-list , enumerator
  • enumerator:
    • enumeration-constant
    • enumeration-constant = constant-expression

So in MONDAY = 0 the whole thing is the enumerator, but only the MONDAY part is the enumerator constant.

If yes, then what does the scope of an enumeration constant mean?

enum {
   A = B,  // Invalid enumerator, B out of scope
   B,      // Scope of B starts here
   C = B   // Ok, B in scope
} 
like image 113
user694733 Avatar answered Sep 02 '25 17:09

user694733