Link to home
Start Free TrialLog in
Avatar of Sandra-24
Sandra-24

asked on

How do you nest one #define inside another #define?

More specifically how would one include a #ifdef-#endif in a macro so that it will be evaluated not where the macro is defined, but where the macro is invoked.

#define macro #ifdef CONSTANT dosomething() #endif

macro; //this will do nothing
#define CONSTANT
macro; //this will do something

Thanks,
-Sandra
Avatar of AlexFM
AlexFM

This is impossible, but there is another way which gives the same result:

#ifdef CONSTANT
#define macro DoSomething();
#else
#define macro
#endif

Using:

macro

If CONSTANT is defined, preprocessor replaces this line to

DoSomething();

If CONSTANT is undefined, preproceccor replaces this line with empty line.
SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I guess what Sandra wants to do is turn the marco ON and OFF in multiple places in the file.

such as,

void main()
{
#undef CONSTANT
macro; //this will do nothing

#define CONSTANT
macro; //this will do something
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sandra-24

ASKER

Thanks Thomas, that's a neat workaround. I didn't think of using an include file which is sortof like a macro to begin with. You can include it in many places in your code and you can add all the #ifdefs you want to it:)

-Sandra