views:

437

answers:

2

i have to do something like this in C but it works only if I use a char but I need a string how can I do?

#define USER "jack" // jack or queen

#if USER == "jack"
#define USER_VS "queen"
#elif USER == "queen"
#define USER_VS "jack"
#endif

thanks to all!

+2  A: 

Use numeric values instead of strings.

Finally to convert the constants JACK or QUEEN to a string, use the stringize (and/or tokenize) operators.

Patrick
+4  A: 

I don't think there is a way to do variable length string comparisons completely in preprocessor directives. You could perhaps do the following though:

#define USER_JACK 1
#define USER_QUEEN 2

#define USER USER_JACK 

#if USER == USER_JACK
#define USER_VS USER_QUEEN
#elif USER == USER_QUEEN
#define USER_VS USER_JACK
#endif

Or you could refactor the code a little and use C code instead.

Brian R. Bondy
instead of defining USER as 1, define USER as USER_JACK.
Patrick
@Patrick: agree fixed.
Brian R. Bondy