#define ADD(a,b) (a)+(b)
void foo1()
{
int a=1, b=2;
int k=0;
while(k++ < 10000)
{
int c = ADD(a,b);
a = c;
}
}
void foo2()
{
int a=1, b=2;
int k=0;
while(k++ < 10000)
{
int c = a + b;
a = c;
}
}
what is the difference between foo1 and foo2. Someone asked me this question and I could not find any difference. both seem 100% same. Maybe some difference regarding memory allocation on stack? you may try to answer the question with the assumption that memory is very limited?
As far as the C compiler is involved, there is just a tiny little less work for it to do.
One of the steps in compiling source files is the preprocessing. One of the steps of preprocessing is expanding the macro.
When you expand the macro by hand, the compiler doesn’t have to do it.