user3199929
user3199929

Reputation:

Is it possible to use one preprocessor's input into another preprocessor in C?

I am trying to do something like this...

#define INPUT (x = 3, y = 5)
#define MATH(add) ((add == 1) ? (INPUT.x + INPUT.y) : (INPUT.x - INPUT.y))

void main (void)
{
int add = MATH (1);
int subs = MATH (0);
}

Basically I want to simplify this...

#define x 3
#define y 5
#define MATH(add) ((add == 1) ? (x + y) : (x - y))

void main (void)
{
int add = MATH (1);
int subs = MATH (0);
}

Is this possible in C somehow?

Upvotes: 0

Views: 57

Answers (1)

chqrlie
chqrlie

Reputation: 144740

Since you want to do cpp drugs so badly, here is some food for thought:

#include <stdio.h>

#define x 3
#define y 5
#define x1 +
#define x0 -
#define MATH(a) x x##a y

int main(void) {
    int add = MATH(1);
    int subs = MATH(0);

    printf("add=%d, sub=%d\n", add, subs);
    return 0;
}

Upvotes: 2

Related Questions