This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

enum data type

my doubt is a general C doubt.. we know if we are using enum the variables which we declare inside automatically increments by one than the previous variable.. but is there any method by which we could make the variables to decrement by one instead of incrementing...

Example

enum my_enum
{ var1=90, var2,var3
};

for this code var2 and var3 will be 91 & 92 respectively, is there any method (possible) to get them 89 & 88...
It was asked in an interview.. any one knows the answer..?

Parents
  • typedef enum X
       {
       base     = 90,          // 0x5a
       primus   = base - 1,    // 0x59
       secundus = primus - 1,  // 0x58
       tertius  = tertius - 1  // 0x57
       } X;
    


    If you're going to write out the values anyway in the comments, you might as well just assign them directly rather than have to do both that and mess about with the decrementing scheme.

    This scheme has the double disadvantage that the comments can get out-of-step with the real values, and you can get the decrementing scheme wrong as well!

Reply
  • typedef enum X
       {
       base     = 90,          // 0x5a
       primus   = base - 1,    // 0x59
       secundus = primus - 1,  // 0x58
       tertius  = tertius - 1  // 0x57
       } X;
    


    If you're going to write out the values anyway in the comments, you might as well just assign them directly rather than have to do both that and mess about with the decrementing scheme.

    This scheme has the double disadvantage that the comments can get out-of-step with the real values, and you can get the decrementing scheme wrong as well!

Children
  • I would either go for explicit numbers in the enumerator, or rearrange the symbols to make use of the normal increment.

    But on the other hand, I normally don't spend too much time debugging, where I need to look at the raw numbers in the debugger.

    If doing embedded, I often try to get a prototype built with extra memory, so I can have a bit of helper info available.

    A trick I do use when playing with enumerators is to:

    /* operators.h */
    T(ADD)
    T(SUB)
    T(MUL)
    T(DIV)
    

    #define T(x) x,
    enum OP { #include "operators.h"
    }; #undef T
    #define T(x) #x,
    char* op_names[] = { #include "operators.h"
    }; op = ADD;
    printf("operator %s\n",op_names[op]);

  • and i've got a question about i2c function.
    1) is it true that i can set any fixed baud rate (clock) from 0 to 100KHz?
    2) if i set the bit I2CFREQ, what value should i put into the register if i'm using crystal 4MHz?
    4) if i set the bit I2CFREQ2,SMP=0, is the baud rate fixed to 400KHz or i'm free to set any baud rate from 0 to 400KHZ? what value should i put into I2CADD then using a 20MHZ crystal?

    answers are appreciated.
    Thank you.

  • Why do you think this is an appropriate question in a thread about the "enum data type"?

    Does it have anything whatsoever to do with the preceding discussions?!

  • Not only off-topic, but also incomplete. No mention of used processor.