Implementation of enum data type in ANSI C and C++
An enumerated data type is a user-defined data type through which names can be attached to numbers.
The declaration is almost same in Ansi C and C++. Consider following example-
enum shape(circle, rectangle, square, riangle);
enum color(red, green, yellow, white);
In C++ the tag names shape and color becomes new type name. By using these tag name we can declare new variables. Examples-
shape hyperbola; //hyperbola is of type shape
color foreground; //foreground is of type color
But In Ansi C it would be like this..
enum shape hyperbola; //hyperbola is of type shape
enum color foreground; //foreground is of type color
Ansi C defines the type of enum to be ints, But in C++, enum datatype retain its own separate type. Because of this C++ does not permit an int value to be assigned to the enum variables. This can be done by type conversion.
color foreground =red; // allowd
color foreground=3; //not allowed, the integer needs to be converted in to color type.