Skip to content

Instantly share code, notes, and snippets.

@mdznr
Last active August 29, 2015 13:56
Show Gist options
  • Save mdznr/9241991 to your computer and use it in GitHub Desktop.
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.
// 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
@rgov
Copy link

rgov commented Feb 27, 2014

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.

@mdznr
Copy link
Author

mdznr commented Mar 15, 2014

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