Preprocessor query

  • Thread starter Thread starter Ben Taylor
  • Start date Start date
B

Ben Taylor

Hi,
I know that when I use #define statements, the
preprocessor goes and replaces instances of where I have
used the definitions with what they are defined as, but my
question today is does it evaluate them first (if they
don't rely on variables)?
For instance if I have #define WHITE RGB(255, 255, 255)

does it simply replace instances of the word 'WHITE'
with 'RGB(255, 255, 255)' or does the preprocessor
actually run the RGB macro to work out what the *result*
of RGB(255, 255, 255) is and send that to the compiler?
 
Hello,

Usually, the compiler will evaluate an expression that got immediates into
one result value.
So: WHITE will be replace with RBG(255,255,255) then if RGB is a macro and
is passed all constants then the compiler will evaluate to a constant.

However you can experiment w/ that using the Disassembly window. (vc6 key =
alt+8)

Regards,
Elias
 
The PREprocessor replaces exactly what you type. For example...

#define MULTIPLY(x, y) x * y

z = MULTIPLY(1+2, 3+4)



This becomes

z = 1+2 * 3+4;



The optimizing compiler will precalculate as much as possible. This is what
actually goes into your exe.

z = 11; // Not what you expected?
 
Back
Top