473,383 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Macro to stringify an enum

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
Nov 14 '05 #1
3 11309
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?

--
Er*********@sun.com

Nov 14 '05 #2
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
Nov 14 '05 #3
Eric Sosman <Er*********@sun.com> wrote in message news:<40**************@sun.com>...
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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs...
2
by: Troy Hanson | last post by:
I use this kind of construct sometimes: typedef enum color { red, orange, blue } color; char *colors_strs = { "red", "orange", "blue" }; That way, I can easily print the textual name of...
8
by: Christopher Benson-Manica | last post by:
Given an enumeration something like enum footype { foo=3, bar=5, baz=17, quux=200 }; is there a fast way to access all four of these values? I.e., given
3
by: bob | last post by:
Need help with c macro. Have this call in my c program. stub.c SYMBOL(ALPHA) << no, dont want to change this to any other form. SYMBOL(BETA) Need a macro to expand each of above to this.
6
by: randy1200 | last post by:
The following enum is given to me, and I can't change it: enum yo { ONE, TWO, THREE }; I have the following: char test = "ONE"; Any ideas on how to see if the string in "test" is in the...
1
by: Bob | last post by:
I'm porting a piece of C++ code to C#. It looks something like this: #define MAKE_ENUM(x, y, z) (((x) << 16) | ((y) << 8)) | (z)) enum MyEnum { A = MAKE_ENUM(3, 9, 0), B = MAKE_ENUM(4, 10,...
6
by: Bo Peng | last post by:
Dear list, I am having trouble with a seemingly very simple problem, namely, I need to pass a string to C++ code through macro definition: gcc -DVERSION=0.6.7 test.cpp while in test.cpp ...
3
by: pistmaster | last post by:
Hi, I am trying the use the current line number in my logging and I want to stringify it so that I know the size of the buffer required to output the log string. I would have though I could use...
35
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.