Search code examples
gccconcatenationc-preprocessor

C preprocessor: arithmetic in concatenation


How can I get the preprocessor to do arithmetic in concatenation?

I tried with:

#define DECL_FUNCT3(ch1, ch2) \
        void funct_ ## ch1 ## _and_ ## ch2 ## _(void);

#define DECL_FUNCT2(ch1, ch2) DECL_FUNCT3(ch1, ch2)
#define DECL_FUNCT1(ch1, ch2) DECL_FUNCT2(ch1, ch2)
#define DECL_FUNCT(ch) DECL_FUNCT1(ch, ch+16)

DECL_FUNCT(0)
DECL_FUNCT(1)

I'd like to get:

void funct_0_and_16_(void);
void funct_1_and_17_(void);

but instead I get:

gcc -E test.c

Output:

[...]
void funct_0_and_0+16_(void);
void funct_1_and_1+16_(void);

Is it possible?


Solution

  • No. It's not possible.

    The C preprocessor does only perform textual replacements. The only place where the preprocessor is calculating is like in #if 10 + 20 == 30, but that line does not perform any replacements.