Last active
August 29, 2015 13:56
-
-
Save mdznr/9241991 to your computer and use it in GitHub Desktop.
Wondering if there's a better way to set a value conditionally using C pre-processor macros.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Is there a better way to do this?: | |
static int someIntegerValue = | |
#ifdef SOME_MACRO | |
4 | |
#else | |
2 | |
#endif | |
; | |
// Or even this?: | |
#ifdef SOME_MACRO | |
static int someIntegerValue = 4; | |
#else | |
static int someIntegerValue = 2; | |
#endif |
That would get resolved after the pre-processor, right?
Perhaps I could make another macro to handle this, but even that's not a great solution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not saying it's really any better, but:
static int someIntegerValue = (4 * !!SOME_MACRO) || 2;
In this case,
SOME_MACRO
has to be defined, I don't think it works if it's undefined completely.