Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Macro to stringify an enum

Reply
Thread Tools

Macro to stringify an enum

 
 
Chris
Guest
Posts: n/a
 
      06-02-2004
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"isabled" : \
#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
2isabled
3:Unknown

But the output is
i:Enabled
iisabled
i:Unknown

Anybody know how I can do this in a macro?

Thanks,
Cristov
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-02-2004
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"isabled" : \
> #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
> 2isabled
> 3:Unknown
>
> But the output is
> i:Enabled
> iisabled
> 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?

--


 
Reply With Quote
 
 
 
 
Stephen L.
Guest
Posts: n/a
 
      06-02-2004
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"isabled" : \
> #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
> 2isabled
> 3:Unknown
>
> But the output is
> i:Enabled
> iisabled
> 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
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      06-07-2004
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"isabled" : \
> > #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
> > 2isabled
> > 3:Unknown
> >
> > But the output is
> > i:Enabled
> > iisabled
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make a macro stringify and paste a -D defined arg gry C++ 2 04-14-2012 12:36 PM
Macro to stringify an enum Edward Rutherford C Programming 4 06-07-2011 02:48 AM
preprocessor stringify and #include pete C Programming 11 06-20-2009 01:05 PM
use hash to name params, then stringify values in specific order DJ Stunks Perl Misc 3 01-21-2009 12:38 AM
stringify with embedded quotes Randy Kobes C Programming 3 02-26-2005 09:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57