0

I wanted to know if there was any other way to print enum names. Such as this one which I was trying,

typedef enum {
   Apple,
   Berry,
   Grapes,
} FruitType;

And this enum printing function

void printFruitType(FruitType type)
{
    switch(type)
    {
    case Apple: printf("Apple"); break;
    case Berry: printf("Berry"); break;
    case Grapes: printf("Grapes"); break;
    }
}

But this seems laborious, each time I need to write the same code still I need to change the printf statement to print the proper value.

Moreover, the FruitType enum can grow big, so writing a enum printing function wouldn't be a great way to print enum name as a string.

So, how can I tackle this enum printing problem.

akm
  • 338
  • 2
  • 11
  • no - there is no other way other than hardcoding – 0___________ Dec 28 '21 at 17:18
  • Make an array of the names, at the same time as you create the `enum`. It only takes a few seconds to copy/paste it and edit up the syntax. A lot less time than making a complicated macro system. – Weather Vane Dec 28 '21 at 17:30

0 Answers0