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
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:
- If a function contains a loop. (for, while, do-while)
- If a function contains static variables.
- If a function is recursive.
- If a function return type is other than
void
, and thereturn
statement doesn’t exist in the function body. - If a function contains
switch
orgoto
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.

Embedded Software | Firmware | Linux Devic Deriver | RTOS
Hi, I’m SLR. I am a tech blogger and an Embedded Engineer. I am always eager to learn and explore tech-related concepts. And also, I wanted to share my knowledge with everyone in a more straightforward way with easy practical examples. I strongly believe that learning by doing is more powerful than just learning by reading. I love to do experiments. If you want to help or support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning!