-
-
Save raalkml/2365964 to your computer and use it in GitHub Desktop.
The solution to Linus' question on resolving undefined macros in the preprocessor to 0, as well as resolving defined macros to their values.
This file contains 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
#include <stdio.h> | |
#define CONFIG_FOO 1 | |
#define CONFIG_NOO 0 | |
#define is_set(macro) is_set_(macro) | |
#define macrotest_1 , | |
#define is_set_(value) is_set__(macrotest_##value) | |
#define is_set__(comma) is_set___(comma 1, 0) | |
#define is_set___(_, v, ...) v | |
int main() | |
{ | |
printf("It's %d!\n", is_set(FOO)); | |
if(is_set(CONFIG_FOO)) | |
{ | |
puts("And it's true, yo!"); | |
} | |
if(is_set(CONFIG_NOO)) | |
{ | |
puts("Noooo!"); | |
} | |
if(is_set(CONFIG_NOTEXIST)) | |
{ | |
puts("NOT EXIST"); | |
} | |
#if is_set(CONFIG_FOO) == 1 | |
printf("inside set option\n"); | |
#endif | |
#if is_set(CONFIG_NOO) | |
printf("inside unset option\n"); | |
#endif | |
#if is_set(CONFIG_NOTEXIST) | |
NEVER COMPILED; | |
#endif | |
#if is_set(CONFIG_NOTEXIST) == 0 | |
printf("inside non-existent option\n"); | |
#endif | |
#if !is_set(CONFIG_NOTEXIST) | |
printf("inside non-existent option\n"); | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment