Danijel Danijel - 4 years ago 166
C Question

Token concatenation using ## gives "not a valid preprocessing token"

The following code gives error:

"pasting "f32_Q31" and "(" does not give a valid preprocessing token"

#define INIT_Q(N, name, val) \
name.value = f32_Q##N##(val);

#define f32_Q31(x) f32_Q(31,x)
INIT_Q31(name, val) INIT_Q(31, name, val)
INIT_Q25(name, val) INIT_Q(25, name, val)


Can this be fixed?

Answer Source

Because f32_Q31( is not a single token, but two. The ( is a token in and of itself. The result of concatenation must be a single valid token.

If your intent is to initialize name.value with the expansion of another macro, the following will do:

#define INIT_Q(N, name, val) \
    name.value = f32_Q##N(val);

The macro name is what must be a valid token, not the whole expression.


As Sander De Dycker pointed out, you seem to have also omitted the define from your last two macro definitions:

#define INIT_Q31(name, val)     INIT_Q(31, name, val)
#define INIT_Q25(name, val)     INIT_Q(25, name, val)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download