user1150105
user1150105

Reputation:

Pass several arguments as macro in C

Sorry for bad English.

Suppose the code:

#define FOO(x,y) FOO ## x
#define BAR A, B

FOO(A, B) successfully expanded to FOOA. But when I write FOO(BAR), the C preprocessor (gcc -E) give error

error: macro "FOO" requires 2 arguments, but only 1 given

How I should change FOO macro if I want expand FOO(BAR) to FOOA?

Upvotes: 3

Views: 119

Answers (1)

melpomene
melpomene

Reputation: 85887

#define FOO(X, Y) FOO ## X
#define BAR A, B

#define APPLY(F, X) F(X)

APPLY(FOO, BAR)

or

#define FOO(X) FOO_(X)
#define FOO_(X, Y) FOO ## X
#define BAR A, B

FOO(BAR)

Upvotes: 3

Related Questions