views:

31

answers:

2
for(x;x<crap;x++){
    macro(x,y);
}

how is this handled by preprocessor? is the loop unrolled or something else?

+3  A: 

The macro is expanded before the code is compiled - it doesn't matter whether it's in a loop or anywhere else.

#define macro(x, y) doSomething(x, y)
for(x;x<crap;x++){
    macro(x,y);
}

will expand to:

for(x;x<crap;x++){
    doSomething(x,y);
}

The context surrounding macro(x,y) has no effect on how the preprocessor expands it.

(The preprocessor doesn't even know what programming language you're using - it could be C, Python, Brainfuck or a letter to your bank manager and it would expand macros just the same way.)

RichieHindle
The preprocessor _does_ have to know what language is being used. Before macro replacement, the source has to be decomposed into preprocessing tokens, which are defined by the C and C++ language standards and are in some cases different between the two languages. During evaluation of conditional directives, whether `true` and `false` are handled as boolean values is language-dependent (they are in C++ but they are not in C). There are many other subtleties.
James McNellis
+1  A: 

#define macros can be thought of as a search and replace before compilation action happens. This means whatever your macro equates to will be directly substituded in its reference inside your code. No, the loop is no unrolled.

Shynthriir