Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert enum names to string in c

Tags:

c

string

enums

Is there a possibility to convert enumerator names to string in C?

like image 530
Misha Avatar asked Mar 28 '12 12:03

Misha


People also ask

How do I convert an enum to a string?

There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and second by using toString() method.

How do I convert an enum to a string in C ++?

Use const char* Array to Convert an Enum to a String in C++ enum is a built-in type, which can be used to declare small named integers usually formed as an array. This mechanism provides a less error-prone and more readable way of representing a set of integer values.

Can enum class have methods?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.

What is an enum in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.


1 Answers

One way, making the preprocessor do the work. It also ensures your enums and strings are in sync.

#define FOREACH_FRUIT(FRUIT) \         FRUIT(apple)   \         FRUIT(orange)  \         FRUIT(grape)   \         FRUIT(banana)  \  #define GENERATE_ENUM(ENUM) ENUM, #define GENERATE_STRING(STRING) #STRING,  enum FRUIT_ENUM {     FOREACH_FRUIT(GENERATE_ENUM) };  static const char *FRUIT_STRING[] = {     FOREACH_FRUIT(GENERATE_STRING) }; 

After the preprocessor gets done, you'll have:

enum FRUIT_ENUM {     apple, orange, grape, banana, };  static const char *FRUIT_STRING[] = {     "apple", "orange", "grape", "banana", }; 

Then you could do something like:

printf("enum apple as a string: %s\n",FRUIT_STRING[apple]); 

If the use case is literally just printing the enum name, add the following macros:

#define str(x) #x #define xstr(x) str(x) 

Then do:

printf("enum apple as a string: %s\n", xstr(apple)); 

In this case, it may seem like the two-level macro is superfluous, however, due to how stringification works in C, it is necessary in some cases. For example, let's say we want to use a #define with an enum:

#define foo apple  int main() {     printf("%s\n", str(foo));     printf("%s\n", xstr(foo)); } 

The output would be:

foo apple 

This is because str will stringify the input foo rather than expand it to be apple. By using xstr the macro expansion is done first, then that result is stringified.

See Stringification for more information.

like image 158
Terrence M Avatar answered Sep 21 '22 13:09

Terrence M