Macro to stringify an enum

  • Thread starter Edward Rutherford
  • Start date
E

Edward Rutherford

Hello,

I have enum and I want to turn it into a string using the number I've
assigned to and concatenating a string to the end of it (or display an
error string for invalid enums):

typedef enum {
Enabled = 1,
Disabled = 2
} State;

#define State_String(x) ( \
(x == Enabled) ? #x":Enabled" : \
(x == Disabled) ? #x":Disabled" : \
#x":Unknown" \
)

int main(void) {
int i = Enabled;
printf("State: %s\n", State_String(i));
i = Disabled;
printf("State: %s\n", State_String(i));
i = Disabled + 1;
printf("State: %s\n", State_String(i));

return 0;
}

I want the output to look like
1:Enabled
2:Disabled
3:Unknown

But the output is
i:Enabled
i:Disabled
i:Unknown

Anybody know how I can do this in a macro?

// EPR
 
S

Stefan Ram

Edward Rutherford said:
Anybody know how I can do this in a macro?

Not exactly this:

#include <stdio.h>
#include <stdlib.h>

#define STATES \
F( 1, ENABLED )\
F( 2, DISABLED )\
/**/

enum state
{
#define F(x,y) y,
STATES
#undef F
};

char const * const name[] =
{
#define F(x,y) #x ":" #y,
STATES
#undef F
};

int main( void )
{ int i;
i = ENABLED; printf( "State: %s\n", name[ i ]);
i = DISABLED; printf( "State: %s\n", name[ i ]); }
 
E

Eric Sosman

Hello,

I have enum and I want to turn it into a string using the number I've
assigned to and concatenating a string to the end of it (or display an
error string for invalid enums):

typedef enum {
Enabled = 1,
Disabled = 2
} State;

#define State_String(x) ( \
(x == Enabled) ? #x":Enabled" : \
(x == Disabled) ? #x":Disabled" : \
#x":Unknown" \
)

int main(void) {
int i = Enabled;
printf("State: %s\n", State_String(i));
i = Disabled;
printf("State: %s\n", State_String(i));
i = Disabled + 1;
printf("State: %s\n", State_String(i));

return 0;
}

I want the output to look like
1:Enabled
2:Disabled
3:Unknown

But the output is
i:Enabled
i:Disabled
i:Unknown

Anybody know how I can do this in a macro?

Off-hand, no. But why not just print the value if you want it?

#define State_String(x) ( \
(x == Enabled) ? "Enabled" : \
(x == Disabled) ? "Disabled" : \
"Unknown" )
...
printf ("State: %d:%s\n", i, State_String(i));
 
G

Gene

That's the X Macro technique; for a fuller explanation see Walter
Bright's article at <http://drdobbs.com/blogs/cpp/228700289>.

This technique was used in gcc sources, at least some years back when
I studied them, to define various structs, enums, string tables, and
the like for syntax trees. I think there were other uses. Don't know
if that's still the case.

Knowing that, when I later had to generate C code to read and write
binary files to communicate in a FORTRAN packed data representation, I
was able to use a pretty elaborate version of this method to generate
the packing and unpacking code and corresponding C structs. It worked
okay although a few of the details were pretty arcane.

In retrospect it might have been more maintainable to use a separate
program to generate the code from an easily parsed representation.
The C preprocessor has limits for this kind of thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top