0

Is there a way to use another char than # when defining preprocessor directives? Example:

Instead of:

#if 1
foo
#endif

Use ?, for example:

?if 1
foo
?endif
João Paulo
  • 6,300
  • 4
  • 51
  • 80
  • 7
    Why on earth do you want this? – klutt Oct 20 '21 at 11:50
  • 1
    Preprocessor your file to replace `?if` to `#if` and then compile. – KamilCuk Oct 20 '21 at 11:51
  • 1
    Not an answer to your question, but kind of suggestion: you may consider [using an alternative preprocessor](https://stackoverflow.com/questions/396644/replacements-for-the-c-preprocessor) for your C source files. It has several pros and cons (one con being an additional build step), but you may decide if it's worth it. – Luca Polito Oct 20 '21 at 12:05

2 Answers2

5

You have two options:

  • Trigraphs: ??=if 1
  • Digraphs: %:if 1

Both of these are 100% standard C, but at the same time considered very bad practice. However, mainstream compilers tend to warn against trigraphs but not for digraphs, so digraphs are perhaps the lesser evil.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    I'd go with digraphs rather than trigraphs. One reason being that C++17 removed support for trigraphs, so that using a C header with trigraphs in C++ would require a parsing/conversion first. Plus, I've used compilers that didn't support trigraphs at all (yea, they weren't 100% standard-compliant for this reason, but still worth noting). – Luca Polito Oct 20 '21 at 12:00
3

There are trigraph sequences that exists to replace certain characters. In particular, the # character can be replaced with ??=. So the following code is valid:

??=if 1
foo
??=endif

You can't however put an arbitrary replacement in place.

dbush
  • 205,898
  • 23
  • 218
  • 273