Skip to content

Instantly share code, notes, and snippets.

@hank
Created April 12, 2012 07:10
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hank/2365372 to your computer and use it in GitHub Desktop.
Save hank/2365372 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.
#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");
}
return 0;
}
@raalkml
Copy link

raalkml commented Apr 12, 2012

Just to complete it:

if is_set(CONFIG_FOO) == 1

printf("inside set option\n");

endif

if is_set(CONFIG_NOO) == 1

printf("inside unset option\n");

endif

if is_set(CONFIG_NOTEXIST) == 1

printf("inside non-existent option\n");

endif

@raalkml
Copy link

raalkml commented Apr 12, 2012

An "#if is_set(CONFIG_FOO)" and "#if is_set(CONFIG_NOO)" work also as one might expect them to (true and false respectively).

@mdesantis
Copy link

can someone explain me how it works please?

@lpereira
Copy link

@ProGNOmers: See torvalds/linux@69349c2 for an explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment