Is this legal ? (non-contiguous values for enums)

Discussion in 'C Programming' started by Bart Simpson, Mar 10, 2006.

  1. Bart Simpson

    Bart Simpson Guest

    typedef enum {
    ACCESS_DENIED = 1,
    ACCESS_READ_ONLY = 2,
    ACCESS_READ_WRITE = 4
    }AccessTypeEnum ;

    Is this legal? - can I have 'gaps' in the enumeration integer values as
    above ?
     
    Bart Simpson, Mar 10, 2006
    #1
    1. Advertising

  2. Bart Simpson wrote:
    > typedef enum {
    > ACCESS_DENIED = 1,
    > ACCESS_READ_ONLY = 2,
    > ACCESS_READ_WRITE = 4
    > }AccessTypeEnum ;
    >
    > Is this legal? - can I have 'gaps' in the enumeration integer values as
    > above ?


    Yes.

    If you don't provide any values, they start at 0. Between any two
    values you provide, the ones without explicit values are incremented by
    1 starting from the last one specified.

    --
    BR, Vladimir
     
    Vladimir S. Oka, Mar 10, 2006
    #2
    1. Advertising

  3. "Vladimir S. Oka" <> writes:
    > Bart Simpson wrote:
    >> typedef enum {
    >> ACCESS_DENIED = 1,
    >> ACCESS_READ_ONLY = 2,
    >> ACCESS_READ_WRITE = 4
    >> }AccessTypeEnum ;
    >>
    >> Is this legal? - can I have 'gaps' in the enumeration integer values as
    >> above ?

    >
    > Yes.
    >
    > If you don't provide any values, they start at 0. Between any two
    > values you provide, the ones without explicit values are incremented by
    > 1 starting from the last one specified.


    You can even have repeated values:

    enum foo {
    foo_first = 0,
    zero = 0,
    one = 1,
    two = 2,
    foo_last = 2
    };

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 10, 2006
    #3
  4. Keith Thompson wrote:
    > "Vladimir S. Oka" <> writes:
    > > Bart Simpson wrote:
    > >> typedef enum {
    > >> ACCESS_DENIED = 1,
    > >> ACCESS_READ_ONLY = 2,
    > >> ACCESS_READ_WRITE = 4
    > >> }AccessTypeEnum ;
    > >>
    > >> Is this legal? - can I have 'gaps' in the enumeration integer values as
    > >> above ?

    > >
    > > Yes.
    > >
    > > If you don't provide any values, they start at 0. Between any two
    > > values you provide, the ones without explicit values are incremented by
    > > 1 starting from the last one specified.

    >
    > You can even have repeated values:
    >
    > enum foo {
    > foo_first = 0,
    > zero = 0,
    > one = 1,
    > two = 2,
    > foo_last = 2
    > };


    I didn't know that. Do you have an example of how it can be useful?

    --
    BR, Vladimir
     
    Vladimir S. Oka, Mar 10, 2006
    #4
  5. Bart Simpson

    Sandeep Guest

    Vladimir S. Oka wrote:

    > > You can even have repeated values:
    > >
    > > enum foo {
    > > foo_first = 0,
    > > zero = 0,
    > > one = 1,
    > > two = 2,
    > > foo_last = 2
    > > };

    >
    > I didn't know that. Do you have an example of how it can be useful?


    A possible Usage :

    enum err_codes
    {
    err_foo = -1,
    err_bar = -1,
    err_success = 0
    };

    For clarity , you might want to provide named constants with same value
    but different names. The above enum can be used to return the error
    code using different name.
     
    Sandeep, Mar 10, 2006
    #5
  6. "Vladimir S. Oka" wrote:
    >
    > Keith Thompson wrote:
    > > "Vladimir S. Oka" <> writes:
    > > > Bart Simpson wrote:
    > > >> typedef enum {
    > > >> ACCESS_DENIED = 1,
    > > >> ACCESS_READ_ONLY = 2,
    > > >> ACCESS_READ_WRITE = 4
    > > >> }AccessTypeEnum ;
    > > >>
    > > >> Is this legal? - can I have 'gaps' in the enumeration integer values as
    > > >> above ?
    > > >
    > > > Yes.

    [...]
    > > You can even have repeated values:
    > >
    > > enum foo {
    > > foo_first = 0,
    > > zero = 0,
    > > one = 1,
    > > two = 2,
    > > foo_last = 2
    > > };

    >
    > I didn't know that. Do you have an example of how it can be useful?


    enum item_list {
    item_lowest = 0,
    item_foo = 0,
    item_bar = 1,
    item_foobar = 2,
    item_highest = 2
    };

    You could then, for example, check a list with a for loop going from
    "item_lowest" to "item_highest", and not have to worry about adding
    additional items in the future.


    Question: can an enum entry be based on another entry of the same enum?
    For example:

    enum item_list {
    item_lowest,
    item_foo = item_lowest,
    item_bar,
    item_foobar,
    item_highest = item_foobar
    };

    (This "works" on my system, but we all know how reliable "it works on
    my system" is for generalizations.)

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Mar 10, 2006
    #6
  7. Bart Simpson

    Michael Mair Guest

    Vladimir S. Oka schrieb:
    > Keith Thompson wrote:
    >
    >>"Vladimir S. Oka" <> writes:
    >>
    >>>Bart Simpson wrote:
    >>>
    >>>>typedef enum {
    >>>> ACCESS_DENIED = 1,
    >>>> ACCESS_READ_ONLY = 2,
    >>>> ACCESS_READ_WRITE = 4
    >>>>}AccessTypeEnum ;
    >>>>
    >>>>Is this legal? - can I have 'gaps' in the enumeration integer values as
    >>>>above ?
    >>>
    >>>Yes.
    >>>
    >>>If you don't provide any values, they start at 0. Between any two
    >>>values you provide, the ones without explicit values are incremented by
    >>>1 starting from the last one specified.

    >>
    >>You can even have repeated values:
    >>
    >>enum foo {
    >> foo_first = 0,
    >> zero = 0,
    >> one = 1,
    >> two = 2,
    >> foo_last = 2
    >>};

    >
    > I didn't know that. Do you have an example of how it can be useful?


    If you have logical subranges in the enumeration constants,
    then you often find the equivalent of the above, i.e.

    enum foo {
    EFooInvalid,

    EFooErrStart,
    EFooErrBar = EFooErrStart,
    ....
    EFooErrBaz,
    EFooErrEnd = EFooErrBaz,

    EFooWarnStart,
    EFooWarnQux = EFooWarnStart,
    ....

    EFooSuccessStart,
    EFooOK = EFooSuccessStart,
    EFooFullSuccess,
    EFooSuccessEnd = EFooFullSuccess,

    EFooMaxReturn = ESuccessEnd,

    EFooNumReturnCodes
    };

    int isFooWarningReturn (enum foo retval)
    {
    return (EFooWarnStart <= retval)
    && (retval <= EFooWarnEnd);
    }

    I usually use separate values for the "Ends".
    This can make sense for bitranges, too.
    E.g.
    enum FooOffsets {
    EFoStartWobble = 0,
    EFoIsRoundWobble = 0,
    ...
    EFoWobbleTypeBit3,
    EFoEndWobble,

    EFoStartWibble = EFoEndWobble,
    EFoIsSquareWibble = EFoStartWibble,
    ....
    EFoEndWibble,
    ....
    };

    #define CONCAT(a, b) a##b
    #define FOO_MASK(WKind) ((1 << CONCAT(EFoEnd, WKind)) \
    - (1 << CONCAT(EFoEnd, WKind)))

    These are obviously made-up but should give an impression
    of what you can abuse this for :)

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Mar 10, 2006
    #7
  8. Bart Simpson

    Ben Pfaff Guest

    Kenneth Brody <> writes:

    > Question: can an enum entry be based on another entry of the same enum?


    Yes (as long as dependents follow dependencies).
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
     
    Ben Pfaff, Mar 10, 2006
    #8
  9. Bart Simpson

    Michael Mair Guest

    Kenneth Brody schrieb:
    > Question: can an enum entry be based on another entry of the same enum?
    > For example:
    >
    > enum item_list {
    > item_lowest,
    > item_foo = item_lowest,
    > item_bar,
    > item_foobar,
    > item_highest = item_foobar
    > };
    >
    > (This "works" on my system, but we all know how reliable "it works on
    > my system" is for generalizations.)


    Yep. An enumeration constant is "visible" as soon as it is declared.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Mar 10, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McDougall

    Non-contiguous port vector ranges???

    Mark McDougall, Nov 2, 2006, in forum: VHDL
    Replies:
    2
    Views:
    821
    Jim Lewis
    Nov 2, 2006
  2. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,192
    Keith Thompson
    Jan 20, 2007
  3. Niv
    Replies:
    8
    Views:
    452
    Brian Drummond
    Oct 30, 2007
  4. DM
    Replies:
    6
    Views:
    184
    Jon Ericson
    Oct 20, 2004
  5. Öö Tiib
    Replies:
    3
    Views:
    185
    Gerhard Fiedler
    Aug 24, 2013
Loading...

Share This Page