Implementation of enum data type in ANSI C and C++

Asked By 40 points N/A Posted on -
qa-featured

Describe the difference in the implementation of Enum data type in ANSI C and C++.

SHARE
Answered By 0 points N/A #124860

Implementation of enum data type in ANSI C and C++

qa-featured

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);

First difference

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

Second difference

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.

For example-

color  foreground =red;    // allowd

color  foreground=3;        //not allowed, the integer needs to be converted in to color type.

Login/Register to Answer

Related Questions