Question

I want to use the value of an enum like that:

enum state{
STATE_OK =0,
STATE_KO =1};

inside a macro like that:

#define DISPLAY_STATE_OK "StateOK="STATE_OK

And I want to use it like this:

printf(DISPLAY_STATE_OK);

To print: StateOK=0

This is a basic example to explain my need. But it does work, any idea to do it? Maybe by replacing enum by a macro #define STATE_OK 0 (not "0" because I need to use it as an integer). But it does work too.

Thanks in advance for your help.

Était-ce utile?

La solution

You can use the stringify macro operator #.

#define STATE_OK (0)

#define STRINGIFY_2(s) STRINGIFY(s)
#define STRINGIFY(s) #s
#define DISPLAY_STATE_OK "StateOK=" STRINGIFY_2(STATE_OK)

But this only works if you #define the values instead of using an enum.

Autres conseils

Instead of:

#define DISPLAY_STATE_OK "StateOK="STATE_OK

Perhaps:

#define DISPLAY_STATE_OK "StateOK=%d", STATE_OK
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top