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

Aspect Macros Inline Functions
Definition Created using #define preprocessor directive Created using the inline keyword in the function declaration
Type Safety No type checking; direct text substitution Type checking of arguments and return value
Function Call Overhead No function call overhead; direct text substitution Avoids function call overhead
Argument Evaluation Arguments evaluated every time they are used Arguments evaluated just like regular functions
Return Statement No return statement; direct text substitution Can have a return statement
Debugging Can be harder to debug due to direct text substitution Easier to debug since they behave like functions
Scope No scope for variables; potential side effects Has its own scope for variables
Conditional Compilation Not applicable since inlining is a compiler decision Arguments are evaluated every time they are used
Loop and Recursion Suitable for loops and recursion Not suitable for loops and recursion
Code Size Conditional compilation is possible using #ifdef This can result in a smaller code size if inlined
Code Readability Can be less readable due to macro expansion Usually 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 Tutorials C Programming Tutorials
FreeRTOS Tutorials NuttX RTOS Tutorials
RTX RTOS Tutorials Interrupts Basics
I2C Protocol – Part 1 (Basics) I2C Protocol – Part 2 (Advanced Topics)
STM32 Tutorials LPC2148 (ARM7) Tutorials
PIC16F877A Tutorials 8051 Tutorials
Unit Testing in C Tutorials ESP32-IDF Tutorials
Raspberry Pi Tutorials Embedded Interview Topics
Reset Sequence in ARM Cortex-M4 BLE Basics
VIC and NVIC in ARM SPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader Tutorials Raspberry PI Pico Tutorials
STM32F103 Bootloader Tutorials RT-Thread RTOS Tutorials
Zephyr RTOS Tutorials – STM32 Zephyr RTOS Tutorials – ESP32
AUTOSAR Tutorials UDS Protocol Tutorials
Product Reviews STM32 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