Print text instead of value from C enum

前端 未结 12 1016
借酒劲吻你
借酒劲吻你 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:35

    enums in C don't really work the way you're expecting them to. You can think of them kind of like glorified constants (with a few additional benefits relating to being a collection of such constants), and the text you've written in for "Sunday" really gets resolved to a number during compilation, the text is ultimately discarded.

    In short: to do what you really want you'll need to keep an array of the strings or create a function to map from the enum's value to the text you'd like to print.

提交回复
热议问题