Is there a way to do check for numerical equality in macros?
I want to do something like
#define choice 3
#if choice == 3
....
#endif
#if choice == 4
...
#endif
Does C macros have support for things like this?
Is there a way to do check for numerical equality in macros?
I want to do something like
#define choice 3
#if choice == 3
....
#endif
#if choice == 4
...
#endif
Does C macros have support for things like this?
Indeed that should work. See http://gcc.gnu.org/onlinedocs/cpp/If.html#If
That reference is accurate, but written in "standards format": abstractly without examples.
Another way to write your code uses chained #elif
directives:
#if choice == 3
...
#elif choice == 4
...
#else
#error Unsupported choice setting
#endif
Note that if choice
is not #defined
, the compiler (preprocessor) treats it as having the value 0
.
#define MY_MACRO Hello
. How can I make if
to check its string value? –
Orthodoxy Indeed that should work. See http://gcc.gnu.org/onlinedocs/cpp/If.html#If
That reference is accurate, but written in "standards format": abstractly without examples.
As far as i know that should work. What compiler are you using ?
PS : Just for information, the defines names are usually written in caps !
#define CHOICE 3
© 2022 - 2024 — McMap. All rights reserved.