Thread: what is meaning of enum in C

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    what is meaning of enum in C

    I know the data type like int, char, flot.etc enum is also data type I have some doubt on enum.
    Code:
    #include <stdio.h>
    
    enum color { red, green, blue };
    
    enum  color favorite_color;
    
    int main()
    {
        favorite_color = blue;
        
        printf("colour %d",favorite_color);
        
        return 0;
    }
    I think color is name of enum and favorite_color is variable where we can store value red, green and blue in that variable

    What is basic of enum and where it is used ?
    Last edited by abhi143; 10-28-2017 at 11:07 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    An enumerated constant is a list of constants that have their own type name. Basically, it is another way to define constants, particularly if you don't really care what the values are as opposed to what they represent. For example: telling program options apart. The example uses favorite colors, and you have the option of red, green, or blue. By default, red is 0, green is 1, and blue is 2, and if there were more options, they would be 3, 4, 5, and so on.

    You can define the constants with assignment though. Sometimes you can opt to define constants through enum purely for organizational purposes as well. An example would be if I was writing some network code - more likely a network library - and I needed a bunch of HTTP status constants.
    Code:
    enum Http_Status { OK = 200, FORBIDDEN = 403, PAGE_NOT_FOUND = 404, INTERNAL_SERVER_ERROR = 500 };
    That's not all of them but you get the idea. HTTP response status codes - HTTP | MDN

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by whiteflags View Post
    . By default, red is 0, green is 1, and blue is 2, [/code]
    Thanks @whiteflags, Look at this program
    Code:
    #include <stdio.h>
     
    enum color { red, green, blue };
     
    enum  color favorite_color;
     
    int main(void)
    {
        if (favorite_color = 0)
        {
            printf("\n color is red ");
        }
        
        if (favorite_color = 1)
        {
           printf("\n color is green ");
        }
        
         if (favorite_color = 2)
        {
          printf("\n clor is blue ");
        }
        
        return 0;
    }
    Result
    color is green
    clor is blue

    red = 0 which is true condition. why does not print color is red ?

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    = is assignment. == is a comparison operator.

    Code:
    if (favorite_color = 0)
    is always false as favorite color becomes 0 by assignment.
    Code:
    if (favorite_color = 1)
    is always true for the same reason.
    Code:
    if (favorite_color = 2)
    is always true as well.
    Last edited by jack jordan; 10-29-2017 at 01:54 AM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Because assigning zero there would be treated as false anyways.

    The way that you are doing it is treated as though it were a mistake.
    Code:
    C:\Users\jk\Desktop>gcc -Wall color.c 
    color.c: In function 'main':
    color.c:9:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         if (favorite_color = 0)
         ^
    color.c:14:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         if (favorite_color = 1)
         ^
    color.c:19:6: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
          if (favorite_color = 2)
          ^

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    129
    @whiteflags still not clear, why assigning zero would be treated as false.

    @jack I don't think there is matter of logical operator. we just need to look weather condition is true or false

    Code:
    #include <stdio.h>
      
    enum color { red = 1, 
                 green = 2, 
                 blue = 3};
      
    enum  color favorite_color;
      
    int main(void)
    {
        if (favorite_color = 1)
        {
            printf("\n color is red ");
        }
         
        if (favorite_color = 2)
        {
           printf("\n color is green ");
        }
         
         if (favorite_color = 3)
        {
          printf("\n clor is blue ");
        }
         
        return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abhi143
    still not clear, why assigning zero would be treated as false.
    Let's step away from enums for awhile and consider this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 0;
        if (x)
        {
            printf("one\n");
        }
        else
        {
            printf("two\n");
        }
        return 0;
    }
    If you compile and run the above program, you will see the output of "two" because the value of x is 0, which means "false" in the boolean context of an if statement's condition. Now consider:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 123;
        if (x = 0)
        {
            printf("one\n");
        }
        else
        {
            printf("two\n");
        }
        return 0;
    }
    What is happening here is that 0 is assigned to x, and then the result of that expression, i.e., the value of x, is evaluated. Of course, since you just assigned 0 to x, the value of x is 0, hence you end up with the same output as the previous program.

    Quote Originally Posted by abhi143
    I don't think there is matter of logical operator. we just need to look weather condition is true or false
    jack jordan is correct: you used the assignment operator = when it looks like you wanted to use the equality comparison operator == instead. Again, let's step away from enums and consider this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 0;
        if (x = 123)
        {
            printf("one\n");
        }
        else
        {
            printf("two\n");
        }
        return 0;
    }
    If you compile and run the above program, you will see the output of "one". This is because in the condition of the if statement, 123 is assigned to x, and then the result of that expression, i.e., the value of x, is evaluated. Of course, since you just assigned 123 to x, the value of x is 123, and since 123 is not zero, it is "true" in the boolean context of the if statement.

    Now, let's change the use of the assignment operator to be the equality comparison operator:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 0;
        if (x == 123)
        {
            printf("one\n");
        }
        else
        {
            printf("two\n");
        }
        return 0;
    }
    If you compile and run the above program, you will see the output of "two". This is because in the condition of the if statement, the value of x is compared for equality with 123, and since the value of x is 0, and 0 is not equal to 123, the result is 0, i.e., "false" in the boolean context of the if statement.

    Going back to enums, we can apply the above idea: if you want to compare to see if the value of favorite_color is equal to the enum constant red, then you should write:
    Code:
    if (favorite_color == red)
    Note that you could have written:
    Code:
    if (favorite_color == 0)
    but here 0 is a magic number that doesn't convey meaning unlike the use of the enum constant red, which is a motivation for using enums in the first place.
    Last edited by laserlight; 10-29-2017 at 02:51 AM.
    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

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    129
    Thanks everyone Now I got it

    thanks @laserlight for your explanation

  9. #9
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    Hello laserlight, I find your explanation very clear. I Think i have learn't something new, although i have read about it before the explanations given thing was not as clear as you have treated it here. Thanks

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Just a little somthing to look at pretainging to emun
    Code:
    #include <stdio.h>
    enum MyNumsy { FIVE, FOUR, THREE, TWO, ONE };
    
    enum MyNumsy mynumy;
    
    int main (void)
    {
        char d;
        d = THREE;
        printf("my element %d\n", (int)d);
        
        switch (d)
        {
            case 0:
                printf("my element %d\n", (int)d);
                break;
            case 1:
                printf("my element %d\n", (int)d);
                break;
            case 2:
                printf("my element %d\n", (int)d);
                break;
            case 3:
                printf("my element %d\n", (int)d);
                break;
            case 4:
                printf("my element %d\n", (int)d);
                break;
            default:
                break;    
            
        }
        switch (d)
        {
            case TWO:
                printf("my element TWO %d\n", (int)d);
                break;
            case FIVE:
                printf("my element FIVE %d\n", (int)d);
                break;
            case FOUR:
                printf("my element  FOUR %d\n", (int)d);
                break;
            case ONE:
                printf("my element ONE %d\n", (int)d);
                break;
            case THREE:
                printf("my element THREE %d\n", (int)d);
                break;
            default:
                break;    
            
        }
            
    return 0;    
    }
    output
    Code:
    userx@slackwhere:~/bin
    $ ./enum
    my element 2
    my element 2
    my element THREE 2
    Last edited by userxbw; 10-29-2017 at 12:37 PM.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Is there something in particular we are supposed to notice about this?

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    the usage of ENUM with assigning them to a char, then using a switch to get to them and seeing that it does not matter the means you use, be it a number, or the name used in enum, nor the order on puts them in using a switch. so yeah he may lean something from it.

    Oh yeah casting to get the number to print if assigned to a char and or other possibilities.

    because his original code too can even be made into a switch. it is faster as well.
    Last edited by userxbw; 10-29-2017 at 12:43 PM.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    the usage of ENUM with assigning them to a char,
    The correct type to use would be MyNumsy.

    then using a switch to get to them and seeing that it does not matter the means you use, be it a number, or the name used in enum, nor the order on puts them in using a switch. so yeah he may lean something from it.
    Well, it does matter -- you are making guesses as to what may be appropriate to use for this purpose, and have a forgiving compiler. I think it is pretty likely that vendors just use the largest int type available, in order to support the longest lists, and so implicit conversions up and down in size would appear to work. I wouldn't say it is a good habit to get into though. If you need to store an enum constant, give the enum a type name and use that name.

    I don't think there is a well-defined way to print enums, either. The standard says that the range of the integer type used is implementation defined, but for similar reasons as before, you can do things that appear to work like casting to an int. Again though, this is dangerous because it assumes that an int is large enough to represent the constant. So printing the value largely means that you have to know the value already and choose an appropriate result type.

    In fact, the loose language in C regarding enums are why enum classes are in C++ now.

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    The correct type to use would be MyNumsy.


    Well, it does matter -- you are making guesses as to what may be appropriate to use for this purpose, and have a forgiving compiler. I think it is pretty likely that vendors just use the largest int type available, in order to support the longest lists, and so implicit conversions up and down in size would appear to work. I wouldn't say it is a good habit to get into though. If you need to store an enum constant, give the enum a type name and use that name.

    I don't think there is a well-defined way to print enums, either. The standard says that the range of the integer type used is implementation defined, but for similar reasons as before, you can do things that appear to work like casting to an int. Again though, this is dangerous because it assumes that an int is large enough to represent the constant. So printing the value largely means that you have to know the value already and choose an appropriate result type.

    In fact, the loose language in C regarding enums are why enum classes are in C++ now.
    being the programmer that is programming that. I would hope they would know what the value means else it defeats the purpose. as for the rest of it. It depends on the program thus depends on the programmer and what it i doing with the particular program.
    Code:
    $ ls -l /usr/bin/gcc*
    lrwxrwxrwx 1 root root      9 Oct 25 14:35 /usr/bin/gcc -> gcc-5.3.0
    Last edited by userxbw; 10-29-2017 at 01:15 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    being the programmer that is programming that. I would hope they would know what the value means else it defeats the purpose.
    The whole point of enum is to get rid of what would otherwise be a magic value, so that the constant you use (like PAGE_NOT_FOUND) makes you think about what it means rather than what the value is. The value may be specifically assigned or a default based on the position in the list. Once you start printing enums, then you have to worry about what the value is, because you want to represent the value accurately, so we've already defeated the purpose.
    It depends on the program thus depends on the programmer and what it i doing with the particular program.
    What you were doing - unless you've changed your mind between then and now, because you were wrong - is attempting to teach. You were attempting to teach something wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is the meaning of \f ?
    By nerio in forum C Programming
    Replies: 3
    Last Post: 01-04-2016, 01:38 PM
  2. Tic Tac Toe with enum. Assigning a char to an enum value.
    By Iceboxes in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2013, 09:58 AM
  3. Assigning enum types to enum types?
    By see the big C in forum C Programming
    Replies: 10
    Last Post: 12-21-2010, 02:32 AM
  4. *ptr = 0 what is the meaning
    By dayalsoap in forum C Programming
    Replies: 12
    Last Post: 09-18-2010, 02:49 PM
  5. Meaning of %hd
    By SeekerOfWisdom in forum C Programming
    Replies: 1
    Last Post: 01-26-2006, 04:18 PM

Tags for this Thread