enum and C preprocessor

Say I have a list of enumerations I wish to use to select a variable at compile-time:

enum pins
{
        PIN_A=1,
        PIN_B=7,
        PIN_C=6,
}

int VAR1, VAR2, VAR3, VAR4, VAR5, VAR6, VAR7;

#define PIN_TO_VAR(NUM) VAR ## NUM

int main(void)
{
        PIN_TO_VAR(PIN_A)=32;
}

But this doesn't work, because the macro combines VAR ## NUM into VARPIN_A instead of what I wanted -- value of PIN_A, aka 1, to get VAR1.

The usual trick of #define UNWRAP(X) X doesn't manage to get the value out of the enum, either.

Will I have to just convert all my enums into #define's to get macros to substitute them?

Short answer: yes, you'll need to use some tricks with #define to do what you want; values set in an enum aren't recognised by the preprocessor.

An enum is interpreted by the compiler and not the preprocessor. The statement is ignored by the preprocessor and thus something like PIN_A is not any more meaningful, as far as the preprocessor is concerned, in the later code than j would be from a statement like:

int j = 23;

That explains it.

Okay, redone with #define's and it works. Ordinarily I'd avoid preprocessor tricks, except PIN_A defines a value that's used in both preprocessor and expression contexts -- it can be used as an integer like (1<<PIN_A), to define an 8-bit number to be fed into PORTA, or as part of a variable name RA[0-9] to define a member of a bitfield...