Basic question
#define A 5
#define B 10
#define C (A*B)
int var;
var = C;
so here how macros will be expanded, Is it
var = (5*10)
or
var = (50)
My doubt is on the macro expansion. If macros has some calculations (*,-,/,+) on all constant,then will marco is just a in line expansion or it will evaluate the result and post it
In computer programming, a macro (short for "macro instruction"; from Greek μακρο- 'long, large') is a rule or pattern that specifies how a certain input should be mapped to a replacement output. Applying a macro to an input is known as macro expansion.
C Macro Expansion. You learned about C preprocessor directives and its components earlier. These directive are of four types – macro expansion, file inclusion, conditional compilation ,and other miscellaneous directives. The macro expansion is the most common and popular C preprocessor directives.
Macro expansion is carried out as follows. Once macroexpand-1 has determined that a symbol names a macro, it obtains the expansion function for that macro. The value of the variable *macroexpand-hook* is then called as a function of three arguments: the expansion function, the form, and the environment env.
Macro represents a group of commonly used statements in the source programming language. Macro Processor replaces each macro instruction with the corresponding group of source language statements. This is known as the expansion of macros.
Macro expansion is always just a textual transform of the input source code. You should be able to see the code after the pre-processor (the part of the compilation that does the macro expansion) is done; this text is what the compiler proper then works on.
But many compilers do "constant folding" optimization during compilation, that will optimize 5 * 10
to 50
so that it doesn't need to be calculated at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With