top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Which is the difference between inline function and macro function ?

+1 vote
636 views
C: Which is the difference between inline function and macro function ?
posted May 31, 2014 by Ganesh Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

Difference between inline function and macro function is same as difference between function and macro.

To get gyst of difference, try the following code.

#define FUN(a) a+a

int fun(int a)
{
 return a+a;
}

int main()
{
 int a=1;
 FUN(a++);
 fun(a++);
 return 0;
}

In FUN(a++), a will be incremented as many times as it is being used. In fun(a++), it will be incremented once. So, any operation on parameter will be repeated in macro multiple times.

You can debug through inline functions, but not through MACRO.

You can specify the type of parameter in inline function, but not in MACRO.

Using MACRO as function is not recommended. In such cases, do not expect anything good from the debugger.

answer Jun 1, 2014 by Devender Mishra
+2 votes

Check the following WiKi Links http://en.wikipedia.org/wiki/Inline_function#Comparison_with_macros

In Summary -
Inline replaces a call to a function with the body of the function in every place that the function is called however Compilers are not obligated to respect this request. (user always_inline with gcc to force the inline operation)

A macro on the other hand, is expanded by the preprocessor before compilation, so it's just like text substitution, also macros are not type checked where as inline functions are.

answer May 31, 2014 by Salil Agrawal
Similar Questions
+1 vote

Can someone help me with the example when to use normal function calling and when pointer function calling. What was the reason for introducing pointer function calling in C?

...