Preprocessor query

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?
 
L

lallous

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
 
K

Kanon Wood

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top