Thread: What is enumeration

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    What is enumeration

    I was studying my book and suddenly there are something called enumeration pop up. What's that means?

    Code:
    enum Status{ CONTINUE, WON, LOST};

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Enumeration (n): the act of naming one by one.

    An enumerated type, such as the example you show, is one that specifies a number of named values.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by ncode View Post
    I was studying my book and suddenly there are something called enumeration pop up. What's that means?

    Code:
    enum Status{ CONTINUE, WON, LOST};
    Enums are often used to when you have something (i.e. a state) that can be defined by a number, but instead of using the number itself you want a more descriptive expression. One example is a state machine, consider one that has the states on, off and idle, represented by the numbers 1, 2 and 3. Instead of doing this in your code:
    Code:
    int state = 1;
    
    switch(state) {
        case 1:
            /* In state on */
            break;
        case 2:
            /* In state off */
            break;
        case 3:
            /* In state idle */
            break;
    }
    You can be more clear, if you use an enum to describe the states:
    Code:
    enum STATE {
        ON = 1,
        OFF = 2,
        IDLE = 3
    };
    
    enum STATE state = ON;
    
    switch(state) {
        case ON:
            /* In state on */
            break;
        case OFF:
            /* In state off */
            break;
        case IDLE:
            /* In state idle */
            break;
    }
    Last edited by iceaway; 10-13-2011 at 06:48 AM.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Thank you guys so much! Apperciate it so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enumeration
    By webren in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 02:47 PM
  2. Enumeration help
    By CXHatchback in forum C Programming
    Replies: 3
    Last Post: 04-17-2003, 08:59 AM
  3. enumeration
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM
  4. enumeration
    By lockpatrick in forum C Programming
    Replies: 2
    Last Post: 04-03-2002, 02:55 PM
  5. Enumeration in C and C++
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-27-2001, 01:12 PM