Thread: what is meaning of enum in C

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    vlid ways to declare an enum
    Code:
    enum boolean { false, true };
    enum boolean check;
    
    enum boolean 
    { 
       false, true
    }
    
    include <stdio.h>
    
    enum week { sunday, monday, tuesday, wednesday, thursday, friday, saturday };
    
    int main()
    {
        enum week today;
        today = wednesday;
        printf("Day %d",today+1);
        return 0;
    }
    
    enum MyNumsy { FIVE, FOUR, THREE, TWO, ONE };
    
     
    
    enum MyNumsy mynumy;
    
    
    
    
    #include <stdio.h>
    
    enum suit {
        club = 0,
        diamonds = 10,
        hearts = 20,
        spades = 3
    } card;
    
    int main() 
    {
            card = club;
        printf("Size of enum variable = %d bytes", sizeof(card));    
        return 0;
    }
    
    enum designFlags {
        ITALICS = 1,
        BOLD = 2,
        UNDERLINE = 4
    } button;
    C Enumeration (Enum): Examples and Where it is used?

    I use casting to the enum in my debugging by printf which a lot of people use printf rather than a debugger, their
    are more than one way of doing things. If one knows their enum and which number it is then they can check it by
    casing to insure they are getting the right one. which depends on the circumstances of what they are doing. their
    is a valid use for it. showing the use of it by number of the element which the assignment of a different number
    as already been shown, but by using the given number 0 ... whatever the last one is. it shows how it works by giving both
    examples and shows that it does not have to be in order 0 ... 19 etc.. it can be mixed up, because the
    switch works off the value within the variable name, not in sequence. 1,2,3,4,5,6,7,8,9....

    the sequence one writes it in a switch does not matter as shown in the example. so while not knowing what element
    number it is does not have to be know, it does not hurt to know it. it is left up to the programmer to
    know or not to know. Not someone dictating that they have to or not have to know it.

    flexibility is what is had. to assigning a number to an enum and you have to know what that number
    means as given in you example of Http errors. so now flexibility is had? You can either print out the http error
    number, or you have to know what that error number represents to know what error name it is, so you
    can print out that error name, or number.

    so using an enum then assigning the http error 'code' number to each one, then what?

    Code:
    enum Http_Status { OK = 200, FORBIDDEN = 403, PAGE_NOT_FOUND = 404, INTERNAL_SERVER_ERROR = 500 }
    
    if ( FORBIDDEN )
     print(403);
    or one now longer needs to even know what that code number is anymore because they are now using an enum
    instead? I'd think they'd have to better know what is what and what goes to what else they get confused and
    maybe even assigning the wrong code to the wrong name? The way I see it . it all has its place, it is up to the
    programmer how he or she uses it to benefit from it. and I never said one had to print out the element
    number to the which ever element it is. it can be used for self knowledge while working with the code.

    either way your argument over this with me is made invalid by your use of example of http error codes
    Last edited by userxbw; 10-29-2017 at 01:46 PM.

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You know what's better than examples? The standard.
    Quote Originally Posted by C99 6.7.2.2
    4 Each enumerated type shall be compatible with char, a signed integer type, or an unsigned integer type. The choice of type is implementation-defined, (108) but shall be
    capable of representing the values of all the members of the enumeration. The enumerated type is incomplete until after the
    }
    that terminates the list of enumerator declarations
    The way that I read this, you don't know what type of integer will be used to represent the constant. In contrast to maybe other areas of the standard, I think this is clear. Yes, the standard mentions char, but simple values, like 403, won't fit. All we know is that the type chosen will fit all of the values. So when I said that printing enums involves knowing their value, and choosing a type to represent that value accurately, I was right.

    You originally said this:
    Quote Originally Posted by userxbw
    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.
    This is a statement that can be easily misunderstood. It does matter the means that you use. A char is not always OK. An int is not always OK. All I did was clarify your statement.

    The standard can be found here linked from here.

    Quote Originally Posted by userxbw
    I use casting to the enum in my debugging by printf which a lot of people use printf rather than a debugger, their
    are more than one way of doing things. If one knows their enum and which number it is then they can check it by
    casing to insure they are getting the right one. which depends on the circumstances of what they are doing. their
    is a valid use for it. showing the use of it by number of the element which the assignment of a different number
    as already been shown, but by using the given number 0 ... whatever the last one is. it shows how it works by giving both
    examples and shows that it does not have to be in order 0 ... 19 etc.. it can be mixed up, because the
    switch works off the value within the variable name, not in sequence. 1,2,3,4,5,6,7,8,9....
    This is just madness as far as I can tell. Of course switch works mixed up. It evaluates the expression in the parentheses and chooses the best matching case. If it is case THREE then it will be executed, if it is something else, then that will be executed instead, and then any fall-through cases will occur. That is simple and enumerated constants do not change how switch works. No one suggested this but you. If you meant to use the enums in a switch then using the MyNumsy type, and all of the named constants would be very much clearer. As it is, it only looks like you mean to confuse and obfuscate things.
    Last edited by whiteflags; 10-29-2017 at 02:43 PM.

  3. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    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.

    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.
    basically the print of the enum number was for a personal use not production. it shows what number is attached to the name. Too you cannot print out the name if you're programming along and maybe make a mistake somewhere .. just print out the number to see if you're getting the proper enum name. as a means of debugging. as I stated before.

    Code:
    #include <stdio.h>
    
    enum Http_Status { OK = 200, FORBIDDEN = 403, PAGE_NOT_FOUND = 404, INTERNAL_SERVER_ERROR = 500 };
    
    int main()
    {
        size_t d;
        
        d = FORBIDDEN;
        
        printf("Error %d   \n", (int)d);
        
        return 0;
    }
    too seems to give means to give the error code as well.
    output
    Code:
    userx@slackwhere:~/bin
    $ ./enum
    Error 403
    why even assign a number to an error code, if you're not going to use error codes?
    Last edited by userxbw; 10-29-2017 at 08:44 PM.

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by userxbw
    why even assign a number to an error code, if you're not going to use error codes?
    You can use an error code without printing it. Really, printing errors is beside the point.

    I think you are working under the impression that we are arguing much more than we actually are.

    Also, this cast should be unnecessary.
    Code:
        size_t d;
        ...
        printf("Error %d   \n", (int)d);
    d is a size_t, so print it like so:
    Code:
        printf("Error %zu\n", d);
    The manual is your friend.

    The reason why that you would make a whole enum type and never use it to declare variables continues to be a mystery, but I'm done now.
    Last edited by whiteflags; 10-29-2017 at 08:55 PM.

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