Macro to stringify an enum

Discussion in 'C Programming' started by Chris, Jun 2, 2004.

  1. Chris

    Chris Guest

    This is what I want to do, I have enum and I want to turn it into a
    string using the number I've assigned to and concatenting a string to
    the end of it or displaying some 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()
    {
    int i;

    i = Enabled;
    printf("State: %s", State_String(i));
    i = Disabled;
    printf("State: %s", State_String(i));
    i = Disabled + 1;
    printf("State: %s", 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?

    Thanks,
    Cristov
     
    Chris, Jun 2, 2004
    #1
    1. Advertising

  2. Chris

    Eric Sosman Guest

    Chris wrote:
    > This is what I want to do, I have enum and I want to turn it into a
    > string using the number I've assigned to and concatenting a string to
    > the end of it or displaying some 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()
    > {
    > int i;
    >
    > i = Enabled;
    > printf("State: %s", State_String(i));
    > i = Disabled;
    > printf("State: %s", State_String(i));
    > i = Disabled + 1;
    > printf("State: %s", 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?


    It cannot be done by a macro that generates a string
    literal, because the string literal's contents are fixed
    at compile time. The variable `i' takes on different
    values as the program progresses, and the string literal
    cannot change to reflect the changes in `i'.

    If you are willing to change your printf() format
    the problem can be solved:

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

    Why insist on a macro, though? Do you have a special
    reason not to use an ordinary function?

    --
     
    Eric Sosman, Jun 2, 2004
    #2
    1. Advertising

  3. Chris

    Stephen L. Guest

    Chris wrote:
    >
    > This is what I want to do, I have enum and I want to turn it into a
    > string using the number I've assigned to and concatenting a string to
    > the end of it or displaying some 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()
    > {
    > int i;
    >
    > i = Enabled;
    > printf("State: %s", State_String(i));
    > i = Disabled;
    > printf("State: %s", State_String(i));
    > i = Disabled + 1;
    > printf("State: %s", 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?
    >
    > Thanks,
    > Cristov


    I don't think you can with a macro. Also, each
    invocation of `State_String(x)' puts one copy
    of the strings into the program (some compilers
    may combine these, but there's no guarantee).

    Will something like this do what you want (I
    assume you only want to define the enum's names
    only once)?

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

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

    static const char * const states[] = { [Enabled] "Enabled",
    [Disabled] "Disabled",
    [Unknown] "Unknown" };

    #define State_String(x) \
    (x == Enabled || x == Disabled) ? states[ x ] : states[ Unknown
    ]

    int
    main()
    {
    State i;

    i = Enabled;

    printf("State: %d:%s\n", i, State_String(i));
    i = Disabled;
    printf("State: %d:%s\n", i, State_String(i));
    i = Disabled + 1;
    printf("State: %d:%s\n", i, State_String(i));


    return 0;
    }


    -
    Stephen
     
    Stephen L., Jun 2, 2004
    #3
  4. Chris

    Chris Guest

    Eric Sosman <> wrote in message news:<>...
    > Chris wrote:
    > > This is what I want to do, I have enum and I want to turn it into a
    > > string using the number I've assigned to and concatenting a string to
    > > the end of it or displaying some 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()
    > > {
    > > int i;
    > >
    > > i = Enabled;
    > > printf("State: %s", State_String(i));
    > > i = Disabled;
    > > printf("State: %s", State_String(i));
    > > i = Disabled + 1;
    > > printf("State: %s", 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?

    >
    > It cannot be done by a macro that generates a string
    > literal, because the string literal's contents are fixed
    > at compile time. The variable `i' takes on different
    > values as the program progresses, and the string literal
    > cannot change to reflect the changes in `i'.
    >
    > If you are willing to change your printf() format
    > the problem can be solved:
    >
    > #define STATE(x) (\
    > ((x) == Enabled ? "Enabled" : \
    > ((x) == Disabled ? "Disabled" : \
    > ? "Unknown")
    > ...
    > printf ("State: %d:%s\n", i, STATE(i));
    >
    > Why insist on a macro, though? Do you have a special
    > reason not to use an ordinary function?


    Yeah, this is what I ended up doing in the end. I just wanted to keep
    the code simple and readable instead of adding another function and
    function prototype and all that. Figured if there was a way to do it
    all in a macro then that would be good. Changing the printf was the
    best way to do it once I realized that it could not be done simply
    another way.

    Thanks,
    Cristov
     
    Chris, Jun 7, 2004
    #4
    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. Randy Kobes

    stringify with embedded quotes

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

    preprocessor stringify and #include

    pete, Jun 19, 2009, in forum: C Programming
    Replies:
    11
    Views:
    1,479
  3. Edward Rutherford

    Macro to stringify an enum

    Edward Rutherford, Jun 5, 2011, in forum: C Programming
    Replies:
    4
    Views:
    1,587
  4. DJ Stunks
    Replies:
    3
    Views:
    154
    DJ Stunks
    Jan 21, 2009
  5. gry
    Replies:
    2
    Views:
    499
Loading...

Share This Page