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
  • I don't think the interpretation need to be generous. The paragraph seems to give a carte blanche. A signed integer type or unsigned integer type means that any integer size is allowed. And they obviously allow either signed or unsigned.

    I just noted that in the general case, are there any note in the standard that printf() should use %d and not %u? Isn't that one of the non-portable things that people have to be aware of?

    Note that this thread doesn't specify any architecture, so I can't assume that we are talking about the C51 compiler.

Reply
  • I don't think the interpretation need to be generous. The paragraph seems to give a carte blanche. A signed integer type or unsigned integer type means that any integer size is allowed. And they obviously allow either signed or unsigned.

    I just noted that in the general case, are there any note in the standard that printf() should use %d and not %u? Isn't that one of the non-portable things that people have to be aware of?

    Note that this thread doesn't specify any architecture, so I can't assume that we are talking about the C51 compiler.

Children
  • I don't think the interpretation need to be generous. The paragraph seems to give a carte blanche. A signed integer type or unsigned integer type means that any integer size is allowed. And they obviously allow either signed or unsigned.

    Based on the quotation you provided it seemed to me that the intent was that the implementation should choose an integer type, document it and stick to it. I thought that interpreting it to mean that the implementation could select from the available integer types for every enumerated type created was pushing it a bit - but after a bit of research it seems that is exactly what happens.

    I just noted that in the general case, are there any note in the standard that printf() should use %d and not %u? Isn't that one of the non-portable things that people have to be aware of?

    No, but C51 uses either signed char or signed int for enumerated types, so %bd or %d would be appropriate, or just %d and cast the enum to int.

    Note that this thread doesn't specify any architecture, so I can't assume that we are talking about the C51 compiler.

    Indeed, I did qualify my comments as being applicable to C51 though.

    It is also interesting to note that the enumeration constants have type int, so one might expect that any variable of that type would only need to hold the range of int. I don't know exactly what the outcome of this should be:

    enum x{a=MAX_INT, b};

  • Indeed, I did qualify my comments as being applicable to C51 though.

    I read your original text as if the 'b' qualifier was required for the C51 architecture, but that the 'd' should be used for all architectures.

  • I read your original text as if the 'b' qualifier was required for the C51 architecture, but that the 'd' should be used for all architectures.

    My original intention was that the entire comment was applicable to C51, by 'general' I meant that 'd' and a cast to int would work whichever type (char or int) C51 chose to use for the enumeration type.

    However, it seems that the standard requires the enumeration constants to be of type int, so this would suggest to me that the enumeration type itself will only hold values representable as type int (although it may be capable of a wider or narrower range), irrespective what actual integer type is chosen for the enumerated type, or rather, what integer type the enumerated type is 'compatible with'. If my interpretation is correct I would therefore suggest that this:

    //y is a variable of an enumerated type
    printf("%d\n",(int)y);

    is a general solution outwith C51.

    My preference is to avoid enum altogether as it provides a level of abstraction that I don't particularly want.