Enumeration is a process of
2012
Enumeration is a process of
Answer: C. Assigning the legal values possible for a variable — Concept: an enumeration (an enumerated type) is a user-defined type whose definition names, one by one, the values that a variable of that type is intended to…
- A.
Declaring a set of numbers
- B.
Sorting a list of strings
- C.
Assigning the legal values possible for a variable
- D.
Sequencing a list of operators
Attempted by 9 students.
Show answer & explanation
Correct answer: C
Concept: an enumeration (an enumerated type) is a user-defined type whose definition names, one by one, the values that a variable of that type is intended to take. Each name introduced this way becomes a compile-time integer constant, so the purpose of the construct is to spell out and label the legal values of a variable — it does not reserve numeric storage, reorder data, or decide how an expression is evaluated.
Application: the C declaration enum color { RED, GREEN, BLUE }; makes the idea concrete.
enum color { RED, GREEN, BLUE };introduces a new type namedenum colorand, in the same declaration, the three enumeration constantsRED,GREENandBLUE.Because no explicit initialisers are written, the constants take consecutive values starting at zero:
REDis 0,GREENis 1 andBLUEis 2.The declaration
enum color c;then creates a variablecof that type, and the values it is intended to range over are exactly the three names written into the type.Testing
cagainst those names — for exampleif (c == GREEN)— is meaningful precisely because the type already spelled out which valuescis meant to take.A point of precision: C implements an enumerated type over a compatible integer type, so storing a value outside the named list is not rejected at run time; the enumeration states the intended legal value set that readers and
switchdiagnostics rely on, rather than a constraint the language checks.What the declaration performed is therefore the fixing of the legal values possible for a variable, which is what enumeration means: assigning the legal values possible for a variable.
Cross-check: naming what each of the other described processes actually corresponds to in C shows that none of them names the value set of a type.
Process described | What it actually is in C |
|---|---|
Declaring a set of numbers | An object declaration such as |
Sorting a list of strings | A run-time rearrangement of data, performed by |
Sequencing a list of operators | Settled by the precedence and associativity rules of the language: in |
Result: enumeration is the process of assigning the legal values possible for a variable.