cc-preprocessor

How can I define a define in C?


Is it possible to write a #define that defines a #define?

For example:

#define FID_STRS(x) #x
#define FID_STRE(x) FID_STRS(x)
#define FID_DECL(n, v) static int FIDN_##n = v;static const char *FIDS_##n = FID_STRE(v)

But instead:

#define FID_DECL2(n, v) #define FIDN_##n v \
                               FIDS_##n FID_STRE(v)

FID_DECL works fine, but it creates two static variables. Is it possible to make FID_DECL2 work and having define two defines?


Solution

  • No; preprocessing is performed in a single pass. If you want or need more advanced behavior, consider using another tool to preprocess the source, like m4.

    Further, the # in the replacement list (at the beginning of #define FIDN... would be parsed as the # (stringize) operator: the operand of this operator must be a named macro parameter, which define is not.