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

Ask a Question

 

Start Coding Today

   

GeeksQuiz

GeeksforGeeksIDE

Data Structures

Algorithms

Interview Experiences

C Programming

C++ Programming

Java Programming

GATE CS

Books

When are macros in c source code executed?

asked Mar 27 by anonymous

4 Answers

Macros are not executed.

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

e.g

#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 by nihal

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

answered May 1 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;
    printf("Area=%.2f",area);
    return 0; 
}

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

answered May 8 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 by Prateek Chanda
...