Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro Expansion

Tags:

c

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

like image 829
Ni3 B Avatar asked Dec 09 '11 15:12

Ni3 B


People also ask

What is macro expansion?

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.

What are the types of 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.

How macro expansion is done?

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.

What is macro and macro expansion in system programming?

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.


1 Answers

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.

like image 157
unwind Avatar answered Oct 20 '22 22:10

unwind