Hi all, What is the difference between the Inline specifier and Macro definition in 'C' ? Illustration with example,helps a lot. regards kanaks
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.
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
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
No not for code modularity but for faster execution. No they waste more memory as the same function when called multiple times reside multiple times in memory. It is not under stack memory are but substituted as statement of the calling function.
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.