#define ENUM_OR_STRING( x ) #x

Hi,

In the following code,

1
2
3
#undef ENUM_OR_STRING    
#define ENUM_OR_STRING( x ) #x


What does the #x mean? is that a way of returning values from macros

Source: http://www.gamedev.net/topic/437852-c-enum-names-as-strings/
closed account (zb0S216C)
It converts the "x" parameter into a null-terminated string. Whatever is given to "x" when the macro is invoked, the exact value of "x" will be placed into the string. For example:

1
2
ENUM_OR_STRING(Fluffy_Slippers);
ENUM_OR_STRING(2 * X);

Here, both "Fluffy_Slippers", and "2 * X" will be converted to a null-terminated string.

Wazzak
Last edited on
It means, whatever you pass in brackets when calling ENUM_OR_STRING, will be replaced as a string literal in you code.

eg:

cout<<ENUM_OR_STRING(how are you?);

Program output will be:

how are you?

since ENUM_OR_STRING(how are you?) is replaced with "how are you?"
Thanks indeed for you response!

So, basically, the '#' is used to typecast any input to a string, is that it?

Also, I've seen places where a ##x is returned, what does that imply?

ex

#define ENUM_OR_STRING(x) str_##x
Last edited on
Also, I've seen places where a ##x is returned, what does that imply?

Yeah this one means joining the lvalue to the rvalue.

eg:
1
2
3
#define write(a) a##out

write(c)<<"I am a boy!";


will be:
I am a boy!

Because the macro takes an argument(a), and adds it to out. So I passed c into the macro, so what basically comes out is cout.

Hope it HELPS!!!
Thanks indeed!!! Helped a lot!!
Topic archived. No new replies allowed.