Enumerated Type - C and Syntactically Similar Languages

C and Syntactically Similar Languages

The original K&R dialect of the C programming language did not have enumerated types, but they were added in the ANSI standard for C, which became C89. In C, enumerations are created by explicit definitions, which use the enum keyword and are reminiscent of struct and union definitions:

enum cardsuit { CLUBS, DIAMONDS, HEARTS, SPADES }; struct card { enum cardsuit suit; short int value; } hand; enum cardsuit trump;

C exposes the integer representation of enumeration values directly to the programmer. Integers and enum values can be mixed freely, and all arithmetic operations on enum values are permitted. It is even possible for an enum variable to hold an integer that does not represent any of the enumeration values. In fact, according to the language definition, the above code will define CLUBS, DIAMONDS, HEARTS, and SPADES as constants of type int, which will only be converted (silently) to enum cardsuit if they are stored in a variable of that type.

C also allows the programmer to choose the values of the enumeration constants explicitly, even without type. For example,

enum cardsuit { CLUBS = 1, DIAMONDS = 2, HEARTS = 4, SPADES = 8 };

could be used to define a type that allows mathematical sets of suits to be represented as an enum cardsuit by bitwise logic operations.

Typeless languages in the syntactic tradition of C (e.g., Perl or JavaScript) do not, in general, provide enumerations.

Read more about this topic:  Enumerated Type

Famous quotes containing the words similar and/or languages:

    It is surely a matter of common observation that a man who knows no one thing intimately has no views worth hearing on things in general. The farmer philosophizes in terms of crops, soils, markets, and implements, the mechanic generalizes his experiences of wood and iron, the seaman reaches similar conclusions by his own special road; and if the scholar keeps pace with these it must be by an equally virile productivity.
    Charles Horton Cooley (1864–1929)

    The very natural tendency to use terms derived from traditional grammar like verb, noun, adjective, passive voice, in describing languages outside of Indo-European is fraught with grave possibilities of misunderstanding.
    Benjamin Lee Whorf (1897–1934)