Difference Between Macro and Inline Function in C

This article is the continuation of the Series on the C programming tutorial and carries the discussion on C language programming and its implementation. It aims to provide easy and practical examples for understanding the C program. In our last article, we have seen the Preprocessor in C programming. In this tutorial, we are going to discuss the Macro vs Inline function in C programming.

Many C and C++ programming beginners tend to confuse the concept of macros and Inline functions. Often the difference between macro and inline functions is also asked in C interviews.

This article covers the definitions, use cases, and advantages of both macros and inline functions. It provides examples and explanations to clarify their implementation and usage. At the end of the article, we also provided a Macro and Inline functions comparison table.

You can also read, Embedded interview topics, container_of macro in C, stringizing and token pasting operator in C, and pointers in C.

Macro vs Inline Functions in C Language

Concept of Macros in C Programming

Macros are generally used to define constant values that are being used repeatedly in programs. Macros can even accept arguments and such macros are known as function-like macros. We have already discussed it the preprocessor directives in-depth in our last article. Please read that before this.

Concept of Inline Functions in C

Inline functions are those functions whose definition is small and can be substituted at the place where its function call is made. Basically, they are inlined with its function call. Even, there is no guarantee that the function will actually be inlined. The compiler interprets the inline keyword as a mere hint or requests to substitute the code of the function into its function call.

Usually, people say that having an inline function increases performance by saving time of the function call overhead (i.e. passing arguments variables, return address, return value, stack mantle, and its dismantling, etc.) but whether an inline function serves your purpose in a positive or negatively depends purely on your code design and is largely debatable.

The compiler does inlining for performing optimizations. If compiler optimization has been disabled, then inline functions would not serve their purpose and their function call would not be replaced by their function definition. To have GCC inline your function regardless of optimization level, declare the function with the “always_inline” attribute:

void func_test() __attribute__((always_inline));

Example:

#include <stdio.h>

void inline test_inline_func1(int a, int b) 
{
    printf ("a=%d and b=%d\n", a, b);
}

int inline test_inline_func2(int x) 
{
    return x*x;
}

int main() 
{
    int tmp;
    test_inline_func1(2,4);
    tmp = test_inline_func2(5);
    printf("square val=%d\n", tmp);
    return 0;
}

Output:

$ ./inline
a=2 and b=4
square val=25

Now, we will understand how inline functions are defined. It is very simple. Only, we need to specify “inline” keyword in its definition. Once you specify “inline” keyword in its definition, it requests the compiler to do optimizations for this function to save time by avoiding function call overhead. Whenever calling to the inline function is made, the function call will be replaced by the definition of the inline function.

Inline functions advantages over macros

  • Since they are functions, the type of arguments is checked by the compiler whether they are correct or not.
  • There is no risk if called multiple times. But there is risk in macros which can be dangerous when the argument is an expression.
  • They can include multiple lines of code without trailing backslashes.
  • Inline functions have their own scope for variables and they can return a value.
  • Debugging code is easy in the case of Inline functions as compared to macros.

It is a common misconception that inlining always equals faster code. If there are many lines in the inline function or there are more function calls, then inlining can cause a waste of space.

Remember, inlining is only a request to the compiler, not a command. The compiler can ignore the request for inlining. The compiler may not perform inlining in such circumstances as:

  1. If a function contains a loop. (for, while, do-while)
  2. If a function contains static variables.
  3. If a function is recursive.
  4. If a function return type is other than void, and the return statement doesn’t exist in the function body.
  5. If a function contains switch or goto statement.

Difference Between Macro and Inline Functions in C

AspectMacrosInline Functions
DefinitionCreated using #define preprocessor directiveCreated using the inline keyword in the function declaration
Type SafetyNo type checking; direct text substitutionType checking of arguments and return value
Function Call OverheadNo function call overhead; direct text substitutionAvoids function call overhead
Argument EvaluationArguments evaluated every time they are usedArguments evaluated just like regular functions
Return StatementNo return statement; direct text substitutionCan have a return statement
DebuggingCan be harder to debug due to direct text substitutionEasier to debug since they behave like functions
ScopeNo scope for variables; potential side effectsHas its own scope for variables
Conditional CompilationNot applicable since inlining is a compiler decisionArguments are evaluated every time they are used
Loop and RecursionSuitable for loops and recursionNot suitable for loops and recursion
Code SizeConditional compilation is possible using #ifdefThis can result in a smaller code size if inlined
Code ReadabilityCan be less readable due to macro expansionUsually results in more readable and modular code

In our next tutorial, we will see typedef in the C programming.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials – STM32Zephyr RTOS Tutorials – ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
 
 
 
 
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Table of Contents