Print text instead of value from C enum

前端 未结 12 966
借酒劲吻你
借酒劲吻你 2020-12-02 05:50
int main()
{

  enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};

  Days TheDay;

  int j = 0;

  printf(\"Please enter the day of the week (0 to         


        
相关标签:
12条回答
  • 2020-12-02 06:13

    Here's a cleaner way to do it with macros:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define DOW(X, S)                                                         \
        X(Sunday) S X(Monday) S X(Tuesday) S X(Wednesday) S X(Thursday) S X(Friday) S X(Saturday)
    
    #define COMMA ,
    
    /* declare the enum */
    #define DOW_ENUM(DOW) DOW
    enum dow {
        DOW(DOW_ENUM, COMMA)
    };
    
    /* create an array of strings with the enum names... */
    #define DOW_ARR(DOW ) [DOW] = #DOW
    const char * const dow_str[] = {
        DOW(DOW_ARR, COMMA)
    };
    
    /* ...or create a switchy function. */
    static const char * dowstr(int i)
    {
    #define DOW_CASE(D) case D: return #D
    
        switch(i) {
            DOW(DOW_CASE, ;);
        default: return NULL;
        }
    }
    
    
    int main(void)
    {
        for(int i = 0; i < 7; i++)
            printf("[%d] = «%s»\n", i, dow_str[i]);
        printf("\n");
        for(int i = 0; i < 7; i++)
            printf("[%d] = «%s»\n", i, dowstr(i));
        return 0;
    }
    

    I'm not sure that this is totally portable b/w preprocessors, but it works with gcc.

    This is c99 btw, so use c99 strict if you plug it into (the online compiler) ideone.

    0 讨论(0)
  • 2020-12-02 06:16

    TheDay maps back to some integer type. So:

    printf("%s", TheDay);
    

    Attempts to parse TheDay as a string, and will either print out garbage or crash.

    printf is not typesafe and trusts you to pass the right value to it. To print out the name of the value, you'd need to create some method for mapping the enum value to a string - either a lookup table, giant switch statement, etc.

    0 讨论(0)
  • 2020-12-02 06:17

    Enumerations in C are numbers that have convenient names inside your code. They are not strings, and the names assigned to them in the source code are not compiled into your program, and so they are not accessible at runtime.

    The only way to get what you want is to write a function yourself that translates the enumeration value into a string. E.g. (assuming here that you move the declaration of enum Days outside of main):

    const char* getDayName(enum Days day) 
    {
       switch (day) 
       {
          case Sunday: return "Sunday";
          case Monday: return "Monday";
          /* etc... */
       }
    }
    
    /* Then, later in main: */
    printf("%s", getDayName(TheDay));
    

    Alternatively, you could use an array as a map, e.g.

    const char* dayNames[] = {"Sunday", "Monday", "Tuesday", /* ... etc ... */ };
    
    /* ... */
    
    printf("%s", dayNames[TheDay]);
    

    But here you would probably want to assign Sunday = 0 in the enumeration to be safe... I'm not sure if the C standard requires compilers to begin enumerations from 0, although most do (I'm sure someone will comment to confirm or deny this).

    0 讨论(0)
  • 2020-12-02 06:17

    i'm new to this but a switch statement will defenitely work

    #include <stdio.h>
    
    enum mycolor;
    
    int main(int argc, const char * argv[])
    
    {
    enum Days{Sunday=1,Monday=2,Tuesday=3,Wednesday=4,Thursday=5,Friday=6,Saturday=7};
    
    enum Days TheDay;
    
    
    printf("Please enter the day of the week (0 to 6)\n");
    
    scanf("%d",&TheDay);
    
    switch (TheDay)
     {
    
    case Sunday:
            printf("the selected day is sunday");
            break;
        case Monday:
            printf("the selected day is monday");
            break;
        case Tuesday:
            printf("the selected day is Tuesday");
            break;
        case Wednesday:
            printf("the selected day is Wednesday");
            break;
        case Thursday:
            printf("the selected day is thursday");
            break;
        case Friday:
            printf("the selected day is friday");
            break;
        case Saturday:
            printf("the selected day is Saturaday");
            break;
        default:
            break;
    }
    
    return 0;
    }
    
    0 讨论(0)
  • 2020-12-02 06:20

    I like this to have enum in the dayNames. To reduce typing, we can do the following:

    #define EP(x) [x] = #x  /* ENUM PRINT */
    
    const char* dayNames[] = { EP(Sunday), EP(Monday)};
    
    0 讨论(0)
  • 2020-12-02 06:21

    Enumerations in C are basically syntactical sugar for named lists of automatically-sequenced integer values. That is, when you have this code:

    int main()
    {
        enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
    
        Days TheDay = Monday;
    }
    

    Your compiler actually spits out this:

    int main()
    {
        int TheDay = 1; // Monday is the second enumeration, hence 1. Sunday would be 0.
    }
    

    Therefore, outputting a C enumeration as a string is not an operation that makes sense to the compiler. If you want to have human-readable strings for these, you will need to define functions to convert from enumerations to strings.

    0 讨论(0)
提交回复
热议问题