inline Vs Macro

kanaks_go4ex's Avatar, Join Date: Jun 2008
Go4Expert Member
Hi all,

What is the difference between the Inline specifier and Macro definition in 'C' ?
Illustration with example,helps a lot.

regards
kanaks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
There are quite a few

1. Macros are replaced at compile time and Inline functions at runtime.
2. Macros does not type checking where as inline functions can check for parameter data type
3. Inline function may not behave inline depending on its implementation body but such occurance is not possible in Macros

I am not 100% sure with point no 1 but I am sure that the time of replacement of both are different.
0
aditiya's Avatar, Join Date: Aug 2008
Newbie Member
macros are replaced with the expression at the time of compilation but the inline functions are not replaced,the benifit of inline function is that in case of inline function code executed in the same block in which this function is defined ,ctrl is not transferred from one function to another
0
kanaks_go4ex's Avatar, Join Date: Jun 2008
Go4Expert Member
Do we use Inline/Macro both for the code modularity ?

Does inline function definition saves any memory in the system ?

When we declare any function with inline ,it executes in the same block without switching
for the functional calls.Will that block space comes under the stack memory area ?

-thanks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by kanaks_go4ex
Do we use Inline/Macro both for the code modularity ?
No not for code modularity but for faster execution.

Quote:
Originally Posted by kanaks_go4ex
Does inline function definition saves any memory in the system ?
No they waste more memory as the same function when called multiple times reside multiple times in memory.

Quote:
Originally Posted by kanaks_go4ex
When we declare any function with inline ,it executes in the same block without switching
for the functional calls.Will that block space comes under the stack memory area ?
It is not under stack memory are but substituted as statement of the calling function.
0
oogabooga's Avatar
Ambitious contributor
Macros are a very simple textual substitution device.
The textual substitution occurs before compile time,
whereas inline functions are inlined (if at all) at
compile time.

Inline functions have to behave just like regular functions
so their code should not be considered as executing in the
same block as the surrounding code. For instance, they cannot
access variables in the surrounding code, whereas a macro
could.

Inline functions are generally preferred over macros because
of argument type checking and separation from surrounding code.