GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

When are macros in c source code executed?

asked Mar 27, 2017 by anonymous

4 Answers

Macros are not executed.

When you start compiling your code it gets replaced with macro definition.


#define data = 2*2

int main()
int j = data;
printf("%d", j);

In this case once the compilation starts, your code will look like

int main()
int j = 2*2;
printf("%d", j);

However you cannot see this as this is a part of compilation process.

answered Mar 28, 2017 by nihal

Macros will not be executed, it will be replaced at pre-processing stage.

answered May 1, 2017 by vavajodu

macro is a fragment of code which has been given a name. Whenever the name is used in the program, it is replaced by the contents of the macro.
For example :

#include <stdio.h> 
#define PI 3.1415 
int main() 
    float radius, area;
    printf("Enter the radius: ");
    scanf("%d", &radius);  
    area = PI*radius*radius;
    return 0; 

Here, wherever the macro PI is used, it will be replaced by 3.1415 at the pre-processing stage.

answered May 8, 2017 by Ranju Kumari

Macros are not executed.

When you define a macro, then at the time of compilation of the code the variable which you have defined using macro gets replaced by the assigned value and the code gets executed.

Hence macros are quite fast than functions.

Almost like inline functions in case of Object Oriented Programming

answered May 9, 2017 by Prateek Chanda