• C



there is a code.

     typedef enum grade{GOOD,BAD,WORST} AD;
        AD g1;

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
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.

 typedef enum grade{GOOD,BAD,WORST} AD
and AD is a varible defined from grade;

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

BTW all enums are integers

you can also define enum like

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



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


and so on
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

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
 "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
I like Mysida's slant on things but I think he's forgotten that we are talking 'C' here. A common mistake from 'C++' gurus ;)

In short:

g1 = 1;

Is perfectly good C and it will work but it is bad practice because it will almost certainly break easily. What happens when someone goes into teh header file and makes it:

    typedef enum grade{PERFECT,GOOD,BAD,WORST} AD;

Something that will change in functionality just because of its environment is generally a bad thing.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenDBACommented:

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.

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

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.