Inline vs Macro in C

Inline:

  • Is there a big difference between “regular” code and inline code?

Yes and no. No, because an inline function or method has exactly the same characteristics as a regular one, most important one being that they are both type safe. And yes, because the assembly code generated by the compiler will be different; with a regular function, each call will be translated into several steps: pushing parameters on the stack, making the jump to the function, popping the parameters, etc, whereas a call to an inline function will be replaced by its actual code, like a macro.

  • Is inline code simply a “form” of macros?

No! A macro is simple text replacement, which can lead to severe errors. Consider the following code:

#define unsafe(i) ( (i) >= 0 ? (i) : -(i) )

[...]
unsafe(x++); // x is incremented twice!
unsafe(f()); // f() is called twice!
[...]

Using an inline function, you’re sure that parameters will be evaluated before the function is actually performed. They will also be type checked, and eventually converted to match the formal parameters types.

  • What kind of tradeoff must be done when choosing to inline your code?

Normally, program execution should be faster when using inline functions, but with a bigger binary code. For more information, you should read GoTW#33.

 

========================================

Preprocessor macros are just substitution patterns applied to your code. They can be used almost anywhere in your code because they are replaced with their expansions before any compilation starts.

Inline functions are actual functions whose body is directly injected into their call site. They can only be used where a function call is appropriate.

Now, as far as using macros vs. inline functions in a function-like context, be advised that:

  • Macros are not type safe, and can be expanded regardless of whether they are syntatically correct – the compile phase will report errors resulting from macro expansion problems.
  • Macros can be used in context where you don’t expect, resulting in problems
  • Macros are more flexible, in that they can expand other macros – whereas inline functions don’t necessarily do this.
  • Macros can result in side effects because of their expansion, since the input expressions are copied wherever they appear in the pattern.
  • Inline function are not always guaranteed to be inlined – some compilers only do this in release builds, or when they are specifically configured to do so. Also, in some cases inlining may not be possible.
  • Inline functions can provide scope for variables (particularly static ones), preprocessor macros can only do this in code blocks {…}, and static variables will not behave exactly the same way.

 

Sample Program to Understand both:

#define square(x) x*x
int main()
{
  int x = 36/square(6); // Expanded as 36/6*6
  printf("%d", x);
  return 0;
}
// Output: 36

If we use inline functions, we get the expected output. Also the program given in point 4 above can be corrected using inline functions.

inline int square(int x) { return x*x; }
int main()
{
  int x = 36/square(6);
  printf("%d", x);
  return 0;
}
// Output: 1

Author: swamy2064

M. Pavan Kumar Swamy, Completed Advanced course in Embedded Systems From Vector Institute[Hyderabad], From ELURU.

Leave a comment