No, there's no way to do that. Unless you know all the possible values of A, and they are always integers, in which case you can laboriously test each one in turn:
#if A == 0
# define B 0
#elif A == 1
# define B 1
#elif A == 2
# define B 2
/* ... and a very long etc. */
#endif
If your use case only involves integers, you have more options. You could, for example, declare Bto be static const int or enum (depending on language) instead of a macro, which would obviously use the current value of the macro. If you really really want macros, the Boost preprocessing library has an implementation of the laborious sequence of #ifs above (with some cleverness to reduce the number of preprocessor statements needed to log(N) instead of N).
There is no macro substitution in the #define preprocessor directive; this fact is covered by §6.10 para. 7 of the C standard (§16 para. 6 of the C++ standard, with identical wording):
The preprocessing tokens within a preprocessing directive are not subject to macro expansion unless otherwise stated.
In the description of the #if and #include directives, the standard specifies that macro replacement does occur, which is why the #if solution above works (and the Boost implementation, which also uses a computed #include).
Anever has the value 5 or 3, andBnever equals 5, or 3. - Weather Vaneconst int B = A;instead of#define B A- that way it captures the value ofAat that point in the compilation. Another option is to use the non-Standard but widely implemented (gcc, clang, visual C++...)#pragma push_macro("A")to preserve the original value ofA, then pop it before evaluatingB, but that's fiddly and fragile. - Tony Delroyconst intworks. ideone.com/MpiOOz - Erobrere