Link to home
Start Free TrialLog in
Avatar of decentswati
decentswati

asked on

enum

Hi,

there is a code.

     typedef enum grade{GOOD,BAD,WORST} AD;
     main()
    {
        AD g1;
        g1=1;
        printf("%d",g1);
     }

I want to know what do we mean by actually assigning g1=1.
when such thing can be useful?

Thanks.
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

It is useful when you use as:
    g1 = BAD;

or better when you pass it as argument, so you force the caller to pass a valid value.
Avatar of furqanchandio
furqanchandio

hi

 typedef enum grade{GOOD,BAD,WORST} AD
here
good=0
bad=1
worst=2
and AD is a varible defined from grade;

enums are used to make a program listing more readable
as
g1=BAD as compared to g1=1
as mentioned by jamie above

BTW all enums are integers

cheers
you can also define enum like

 typedef enum grade{GOOD=1,BAD,WORST} AD

here
good=1
bad=2
worst=3

or

typedef enum grade{GOOD,BAD=3,WORST} AD

here
good=0
bad=3
worst=4

and so on
g1=1;   might result in an error about use of an anachronism from a compiler
going with the current C++ draft standards; although for backwards compatibility's
sake it is probably just a warning message for now, you should think
of
 "g1 = 1;" as a mis-feature that is very likely to eventually disappear from
 future versions of  C++ compilers.

You need to at least use

g1 = AD(1);

the meaning of which with the definition you have above is g1=BAD;

It is still a bad idea, and I do not know if this second form will last much
longer, because it is dependent on the ordering of the enum
someone later might write

     typedef enum grade{GOOD=5,BAD=10,WORST} AD;

In which case  g1=AD(1);  has undefined results.

An enumerated type is supposed to mean, these names _are_ the values
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kent Olsen

Hi decentswati,

Lots of good advice here.  Expounding just a bit....

Enumerated lists (the enum{} definitions) make code maintenance a lot easier.  Some very large applications would be nearly impossible to maintain were it not for enumeration.

Your sample code makes a fine example.

GOOD is defined as 0.  (enum{} starts at 0.)  BAD and WORST are 1 and 2, respectively.  With only three values to keep track of many programmers won't bother with the enumerated symbols, electing to use the constants 0, 1, and 2.  Part of the code will likely test for 0 (GOOD) as passing, other values as failing.  The program then gets eaten up with code like "if (Grade == 0)".  At best, this is misleading as Grade could be the final evalution (GOOD, BAD, WORST) or an individual score (0, 1, 2, 3, 4, ... 100).  Coding the same test as "if (Grade == GOOD)" is very obvious.

Now let's think about what happens when we add another classification.  Let's call it AVERAGE.  Logically, this would come between GOOD and BAD.  If we insert AVERAGE into the enumerated list at the desired location, all we have to do is recompile the program.  If we've used the labels that we enumerated the rest of the source code remains unchanged.  But if we have hard coded 0, 1, and 2 we must examine the entire source code, and replace SOME of the 1s and 2s with 2s and 3s.  There will likely be places in the code where a 1 or 2 has nothing to do with the grade.  These values won't change just because the grade scale did.

If we've hard-coded the grade values (0, 1, 2) it's very difficult to insert AVERAGE into the middle of the values.  Adding it at the end can make other coding extremely challenging.  If we give AVERAGE a new value (in this case 3) the logical progression of GOOD, AVERAGE, BAD, WORST is broken.  Coding for relative weight (GOOD > AVERAGE > BAD > WORST) is no longer straight-forward.  Instead the tests have to understand that AVERAGE comes between GOOD and BAD.  3 comes between 0 and 1.

As the programs evolved we later need to add ABOVE_AVERAGE and BELOW_AVERAGE.  Later we add BARELY_PASSING.  If we enumerate these values we get GOOD, ABOVE_AVERAGE, AVERAGE, BELOW_AVERAGE, BARELY_PASSING, BAD, WORST.  At any point we can easily test "if (Grade > BARELY_PASSING)".  But if we've hard-coded these values and continue to add to the end of the list, the values are no longer sorted.  If we simply add to the end of the list without resequencing the values for these Grades is 0, 5, 3, 6, 4, 1, 2.  Now we find that relative testing becomes a real challenge.


Use enumerated lists.  They may be a bit of pain now, but they'll certainly be your friend later when you have to maintain the code.

Kent
I want to know what do we mean by actually assigning g1=1.
Answer:
In both C and C++, an enum specifies a type and a corresponding set of named constants. For example:
enum day
  {
  GOOD,BAD,WORST  };

defines a type grade with three constants.
By default, the first enumeration constant has the value 0, and each subsequent constant has the value that is one more than the value of the previous constant. Thus,GOOD 's value is 0, BAD is 1, and so on.
You can specify an explicit value for any enumeration constant. Any enumeration constant without an explicitly specified value has the value one more than the value of the previous constant.


when such thing can be useful?
Enumerations provide a simple yet elegant way to define a set of related symbolic constants as values for a new data type. Using appropriately named enumerated types and constants improves the readability of your code, sometimes dramatically. Moreover, languages such as Ada and C++ treat each enumerated type as distinct from integers and other enumerated types, enabling the compiler to catch a variety of potential errors



>> In both C and C++, an enum specifies a type and a corresponding set of named constants


enums are also available in turbo assembler