Two enums have some elements in common, why does this produce an error?

后端 未结 8 746
难免孤独
难免孤独 2021-01-01 09:17

I have two enums in my code:

enum Month {January, February, March, April, May, June, July,
        August, September, October, November, December};
enum Shor         


        
8条回答
  •  抹茶落季
    2021-01-01 09:46

    My suggestion here is to have just one enum, as they are the same type. If you want short aliases to type less in your code (even if I wouldn't advise you to do so), you can do:

    enum Month {
     January, Jan = January,
     February, Feb = February,
     March, Mar = March,
     April, Apr = April
     May,
     June, Jun = June,
     July, Jul = July,
     ...};
    

    And to have different presentation names (short and long), you should have two distinct string arrays that are indexed by the enum.

    char[MAX_MONTH_NAME_LENGTH][12] month_long_names = {
      "January", "February", ...
    }
    
    char[3][12] short_long_names = {
      "Jan", "Feb", ...
    }
    
    printf("month %d long name is %s, and short name is %s\n", May, long_month_names[May], short_month_names[May]);
    

提交回复
热议问题