Thread: Need a clarification on Enums

  1. #1
    Registered User
    Join Date
    Aug 2013

    Exclamation Need a clarification on Enums

    Hello Friends !!

    I found a fact about enums from the book : C Programming by Dennis-Ritchie : Please make me understand this by means of a suitable code.

    "Although variables of enum types may be declared, compilers need not check that what you store in such a variable is a valid value for the enumeration."

    Regards //

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    That should be quite trivial, e.g.,
    #include <stdio.h>
    enum Foo {Bar, Baz};
    int main(void)
        enum Foo x = 123;
        printf("Valid values of enum Foo: %d, %d\nx = %d\n", Bar, Baz, x);
        return 0;
    If you compile the above program at the highest warning levels possible for your compiler, you might find that your compiler gives no warning whatsoever. Yet, when you run the program, you would get:
    Valid values of enum Foo: 0, 1
    x = 123
    Clearly, 123 is not among the valid values of enum Foo, then x, despite being declared as of the enum Foo enumeration type, can store 123 without complaint from the compiler.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-04-2014, 11:40 AM
  2. Could someone please clarify enums for me
    By samwillc in forum C++ Programming
    Replies: 24
    Last Post: 04-23-2013, 05:54 PM
  3. 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
  4. Enums and Templates
    By einarp in forum C++ Programming
    Replies: 5
    Last Post: 07-19-2006, 05:07 AM
  5. enums!
    By newjamie in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 12:25 PM