3

Is it possible to write custom conditional pre-processor directives in C. for example;

#define _IF_ (condition, explanation) \
       #ifdef condition

Every comment would be great, thanks.

3
  • 1
    In short: No. This is a XY problem. Please explain what it is you want to achieve with this. meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Sep 21, 2019 at 11:23
  • There will be different functions with same name and diversed with #ifdef directives. There will be also a PC program that will read the code and extract same function names to UI interface. At that point, explanation is important, PC will use this explanation on UI. With this explanation, user can select wright function. That's why I'd like to write an pre-processor directive with an explanation. Commented Sep 23, 2019 at 7:56
  • Please edit to add info and use the space it offers for examples and some conceptual code how you would like to use it and what the context looks like. Commented Sep 23, 2019 at 18:29

1 Answer 1

1

Simple Answer

No.

The #define preprocessor directive can not emit another preprocessor directive (like #ifdef) because the #ifdef is not at the start of the line.

Preprocessing directives are lines in your program that start with #. The # is followed by an identifier that is the directive name. For example, #define is the directive that defines a macro. Whitespace is also allowed before and after the #.

The closest I could get to an elegant syntax is this.

#define _IF_(condition,explanation) condition

#if _IF_(1 == 2,"A False Case")
   "This should not appear"
#endif

#if _IF_(1 == 1,"A True Case")
   "This should appear"
#endif

Demonstrated here : https://godbolt.org/z/JFljOm

Although this macro may have a better label.


Janky Answer

If you really need to get this syntax for some reason and you are willing to modify your build system a little.

You can run your code though the preprocessor twice to allow for this double expansion. However, a preprocessor macro also can't emit the # character, so we'll use the ??= trigraph to circumvent this.

// File a.prec

#define _IF_(condition, explanation) \
       ??=if condition
#define _ELSE_ ??=else
#define _ENDIF_ ??=endif

_IF_(1==1,"Yep")
"This Text"
_ENDIF_

_IF_(2==1,"Nope")
"Not This Text"
_ENDIF_

Compiling with just the -E flag gives us. https://godbolt.org/z/sJIScN

??=if 1==1
"This Text"
??=endif

??=if 2==1
"Not This Text"
??=endif

We then need to enable -trigraphs on the main compile step to parse the ??= as a #. Giving us the desired output of : https://godbolt.org/z/-0rqe5

"This Text"

This solution will give you problems with normal includes as using the preprocessor twice isn't safe, so you'll need to use the ??= trigraph on anything you only want to happen in the normal compile step.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.