Enumerate type

This is a discussion on Enumerate type within the C Programming forums, part of the General Programming Boards category; I got compile error (Borland C/C++ 5.1) with enumerate types. Does anyone know what wrong? Here is my C program. ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question Enumerate type

    I got compile error (Borland C/C++ 5.1) with
    enumerate types. Does anyone know what wrong?

    Here is my C program.
    Code:
    #include <stdio.h>
    typedef enum {NONE, OFF, ON} Light;
    typedef enum {PRESS, DEPRESS, NONE} Button;
    typedef enum {RUN, STOP, OFF} State;
    
    int main()
    {
      Light  L;
      Button B;
      State  S;
      /* do computation here...*/
      return 0;
    }
    Also I have been struggling to find the sizeof
    (enum) enumerate type. It always display 4 bytes
    when I use sizeof(enum) regardless of the number
    of identifiers (elements) of enumerate type I created.
    I really appreciate your help.
    Last edited by dv007; 07-15-2002 at 12:14 PM.

  2. #2
    Unregistered
    Guest
    I am not an expert... but shouldn't

    typedef enum {NONE, OFF, ON) Light;

    be

    typedef enum {NONE, OFF, ON} Light;

    ??



    COOL PROGRAMS @ www.akilla.tk

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question

    Thanx for pointed out that little typo.
    you are not expert but I guess you are a guru.
    Please explain why the code above has error???

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Originally posted by dv007
    Thanx for pointed out that little typo.
    you are not expert but I guess you are a guru.
    Please explain why the code above has error???
    K&R page 215
    The identifiers in enumeration type are declared as constants of type int .... .

    so variables of enum type are like integers
    therefore sizeof(Light) should give to the same value as sizeof (int)
    Are you confusing enums with arrays , in case of arrays the sizeof returns total number of bytes taken by the entire array ?
    The one who says it cannot be done should never interrupt the one who is doing it.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    85
    But you know what causing compile error for
    enumerate types I delared in my for program???

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But you know what causing compile error for enumerate types I delared in my for program???
    Code:
    typedef enum {NONE, OFF, ON} Light;
    typedef enum {PRESS, DEPRESS, NONE} Button;
    typedef enum {RUN, STOP, OFF} State;
    Notice that NONE and OFF are used twice each. This is an error as you can only use an enum identifier once in the program. It is usually a good idea to either prefix or postfix your identifiers with the type as well to avoid this little redefinition problem:
    Code:
    typedef enum { LIGHT_NONE,   LIGHT_OFF,      LIGHT_ON }    Light;
    typedef enum { BUTTON_PRESS, BUTTON_DEPRESS, BUTTON_NONE } Button;
    typedef enum { STATE_RUN,    STATE_STOP,     STATE_OFF }   State;
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    85
    Thanx a lot, Prelude.
    But you know why those defined enum types interfere each other?

    set A = {1, 2 ,3}; <=> enum type A
    set B = {3, 4, 5}; <=> enum type B
    set C = {2, 6, 18}; <=> enum type C
    They are completely different set.
    I think Ada or Pascal raise no compile error
    for those IDENTIFIERs in completely different
    enumerate type? Right? Correct me if I'm wrong?
    Can you explain a litle bit more about this
    interference among different enumerate types in C.
    Since Light has none business with State.

    Again, Thanx Prelude.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But you know why those defined enum types interfere each other?
    Because the standard specifies such behavior.

    From the C99 Standard, section 6.7.2.2
    Code:
    Semantics
    3 The identifiers in an enumerator list are declared as constants that have type int and
    may appear wherever such are permitted.107)
    <snip>
    107) Thus, the identifiers of enumeration constants declared in the same scope shall all be distinct from
    each other and from other identifiers declared in ordinary declarators.
    -Prelude
    My best code is written with the delete key.

  9. #9
    TK
    Guest
    Get rid of the typedef in this particular situation. It is detrimental to your program because it only serves to hide the type which is not a good thing when debugging or having someone else read your program. Just a suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21