Macro to stringify an enum

Discussion in 'C Programming' started by Edward Rutherford, Jun 5, 2011.

  1. 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
     
    Edward Rutherford, Jun 5, 2011
    #1
    1. Advertising

  2. Edward Rutherford

    Stefan Ram Guest

    Edward Rutherford <> 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 ]); }
     
    Stefan Ram, Jun 5, 2011
    #2
    1. Advertising

  3. Edward Rutherford

    Eric Sosman Guest

    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
    d
     
    Eric Sosman, Jun 6, 2011
    #3
  4. On 06/05/2011 05:48 PM, Stefan Ram wrote:
    > Edward Rutherford <> 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/228700289>.

    --Joel
     
    Joel C. Salomon, Jun 6, 2011
    #4
  5. Edward Rutherford

    Gene Guest

    On Jun 6, 9:39 am, "Joel C. Salomon" <> wrote:
    > On 06/05/2011 05:48 PM, Stefan Ram wrote:
    >
    > > Edward Rutherford <> 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/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.
     
    Gene, Jun 7, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    Macro to stringify an enum

    Chris, Jun 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    999
    Chris
    Jun 7, 2004
  2. Randy Kobes

    stringify with embedded quotes

    Randy Kobes, Feb 17, 2005, in forum: C Programming
    Replies:
    3
    Views:
    456
    Chris Torek
    Feb 26, 2005
  3. pete

    preprocessor stringify and #include

    pete, Jun 19, 2009, in forum: C Programming
    Replies:
    11
    Views:
    1,399
  4. DJ Stunks
    Replies:
    3
    Views:
    135
    DJ Stunks
    Jan 21, 2009
  5. gry
    Replies:
    2
    Views:
    489
Loading...

Share This Page