Thread: enums this time

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    enums this time

    according to the book i am referring.

    Code:
     
    enum color{ red,blue,green,white};
    
    int x;
    enum color y;
    
    x= blue; //valid. x contains y
    
    y=2   // compiler warning
    y=2 should give warning.. but it is not giving any warning or anything. jst displaying the no. i put with y.


    and

    what is the us of enummerated data types??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > y=2 should give warning
    It will, IF you compile it as C++ code. enums in C++ are distinct from int, and distinct from each other.

    In C, enums are just named integers, so you use them just as you would use an integer.

    Some lint-like tools will however complain when you (ab)use enums in certain ways.

    > what is the us of enummerated data types??
    Readability is one thing, automatically assigned values is another.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    But when i put y=9, it is again not showing any error. 9 is not in the defined set. it shouldn't display it. And if enums are jus named integers in C, the casting integers as enums explicitly has no meaning.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    #define red 0
    #define blue 1
    #define green 2
    #define white 3

    What you basically get is a symbol which is known to the compiler, as opposed to known to the pre-processor.

    Like I said, enums in C are not much better than this.
    They are not as strongly typed as they are in C++.

    > But when i put y=9, it is again not showing any error.
    All enums are ints in C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scoped enums only available with -std=c++0x or std=gnu++0x
    By Brian_of_Bozeat in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2011, 09:11 AM
  2. run time mapping of enums...
    By tummala_005 in forum C Programming
    Replies: 7
    Last Post: 06-15-2011, 12:41 PM
  3. Serialization of Enums
    By khdani in forum C# Programming
    Replies: 1
    Last Post: 04-27-2009, 10:57 AM
  4. Tricking enums
    By Ganoosh in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 02:46 PM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 12:25 PM