[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Macro to stringify an enum

Edward Rutherford

6/5/2011 9:10:00 PM

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
4 Answers

ram

6/5/2011 9:48:00 PM

0

Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> writes:
>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 ]); }

Eric Sosman

6/6/2011 1:30:00 AM

0

On 6/5/2011 5:09 PM, Edward Rutherford wrote:
> 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));

--
Eric Sosman
esosman@ieee-dot-org.invalid

Joel C. Salomon

6/6/2011 1:39:00 PM

0

On 06/05/2011 05:48 PM, Stefan Ram wrote:
> Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> writes:
>> Anybody know how I can do this in a macro?
>
> #define STATES \
> F( 1, ENABLED )\
> F( 2, DISABLED )\
> /**/

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

--Joel

Gene

6/7/2011 2:48:00 AM

0

On Jun 6, 9:39 am, "Joel C. Salomon" <joelcsalo...@gmail.com> wrote:
> On 06/05/2011 05:48 PM, Stefan Ram wrote:
>
> > Edward Rutherford <edward.p.rutherfor...@REMOVETHIS.gmail.com> writes:
> >> Anybody know how I can do this in a macro?
>
> > #define STATES \
> > F( 1, ENABLED )\
> > F( 2, DISABLED )\
> > /**/
>
> That's the X Macro technique; for a fuller explanation see Walter
> Bright's article at <http://drdobbs.com/blogs/cpp/228....

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.