1

Can a macro call a preprocessor command?

For example, can I write something like,

#define PreProcessor(x, y)  #define x ((y)+1)
msc
  • 33,420
  • 29
  • 119
  • 214

1 Answers1

3

It is not possible to expand a macro into something that is also a preprocessor directive, as in ยง6.10.3.4, 3:

The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one...

But, it is possible to conditionally define macro itself:

#if CONDITION_A_IS_MET
#define x ((y)+1)
#else
#define x /*...some other definition*/
#endif

Or use an X-macro:

#define PreProcessor(x) X(x, ((x) + 1))

/*...later*/

#define X(a, b) printf("%d, %d", a, b)
PreProcessor(5) /* Outputs 5, 6 */

To cover most of the common cases for that feature.