In C, an inline function is a type of function where the compiler replaces the function call with its actual code of the function, invoked through the usual function call mechanism. This can improve performance by reducing the overhead of function calls. The inline keyword is used to declare such functions, which are normally small and frequently called.
Syntax
To declare a function as inline, the keyword inline is placed before the function's return type.
C++
inline return_type function_name(parameters) {
// Function body
}
If the function is forward declared, then inline must also be used in that declaration.
Behaviour of Inline Functions
Inline is just a suggestion or request to the compiler, it is not an order that the compiler always have to follow. The behaviour of inline depends on how it is being used and how inline optimization is implemented by the compiler. Let's look at GCC compiler which is one of them most used compiler.
1. Inline without GCC Optimization
GCC implements inline functions as a part of optimization. When inline is used for a function in a C source file and compiled without any optimization flag, it throws an error as shown:
C
#include <stdio.h>
inline int foo() {
return 2;
}
int main() {
int res;
res = foo();
printf("%d", res);
return 0;
}
Output
/usr/bin/ld: /tmp/ccBVKkSP.o: in function `main':
solution.c:(.text+0x12): undefined reference to `foo'
collect2: error: ld returned 1 exit status
This is the effects of how GCC handles inline functions. When compiled, GCC performs inline substitution as part of its optimization process. As a result, the symbol for the function is not created in the symbol table because the code is directly replaced with the function's body during compilation. Please check below assembly code which compiler will generate.

Now, as substitution is not performed, and the symbol is not generated, the linker cannot link the function definition with the call. It then searches for the given function in other translation unit's symbol table (kind of external linkage), which is also not found here, causing the linker error mentioned above.
2. Inline With GCC Optimization
The first solution to above problem is to turn on the GCC optimization using optimization flag passed during program's compilation command.
gcc solution.c -o solution -O1
Any optimization level greater than O0 will turn on the inline optimization in GCC.
C++
#include <stdio.h>
// Inline function in C
inline int foo() {
return 2;
}
int main() {
int res;
// inline function call
res = foo();
printf("%d", res);
return 0;
}
3. Static Inline Function
We can use the static keyword before the inline function. This forces the compiler to treat the function with internal linkage and ensures that it is considered during the linking process, allowing the program to compile and run successfully. Though the inlining still depends on the compiler’s optimization level.
C
#include <stdio.h>
// Inline function in C
static inline int foo() {
return 2;
}
int main() {
int res;
// inline function call
res = foo();
printf("%d", res);
return 0;
}
4. Inline with Forward Declaration
If the function is declared separately, then it will be added to the symbol table. Later on, it can be defined as inline, and the compiler will consider it for inlining if the optimization level is O1 or above. But if the optimization is O0, then this function will not be inlined but still will be able to be executed as a normal function.
C
#include <stdio.h>
// Forward declaration
int foo();
// Inline function in C
inline int foo() {
return 2;
}
int main() {
int res;
// Inline function call
res = foo();
printf("%d", res);
return 0;
}
5. Extern Inline Function
If the function is defined with external linkage, then the compiler will try to find its definition in other translation units. If found, inlining will still depend on the optimization flag. But the function will be executed in both cases. It will not throw an error.
At the end of the day, inlining is dependent on the compiler optimization schemes. All the other cases are just fallback measures to keep the program from throwing the error.
Inline Function in Other Compilers
Inline functions in C work differently with other compilers. In GCC and Clang, the inline keyword suggests inlining, but the function may not always be inline unless optimizations are enabled. MSVC uses inline too, but to force inlining, you must use __forceinline. The behaviour can vary depending on compiler settings, but all compilers try to inline small functions for better performance when possible.
Inline vs Normal Function
There are several key differences between inline and normal functions some of which are shown below.:
Inline Function | Normal Function |
---|
Defined with the inline keyword | Defined without the inline keyword |
Function call is replaced by the function’s code (inline substitution) | Function call involves a normal call with a stack push |
Can improve performance by eliminating function call overhead | May have overhead due to function call and return mechanisms |
May increase binary size if overused (due to repeated code) | Doesn’t affect code size as much, uses memory only for function calls |
Inline Function vs Macro
An inline function is similar to a macro in that it can substitute the function call with actual code. However, there are some key differences between inline functions and macros:
Inline Function | Macro |
---|
Type-checked, respects data types. | No type-checking can lead to unexpected results. |
Easier to debug, behaves like a regular function. | Harder to debug, errors may not be clear. |
Parameters are evaluated once. | Parameters may be evaluated multiple times. |
Can be recursive. | Cannot be recursive. |
When to use Inline Functions?
The use of inline function is preferred for:
- Small Functions: Inline functions are best suited for small, frequently used functions where the performance improvement from avoiding function calls is significant.
- Time-Critical Code: Use inline functions in scenarios where performance is critical, such as embedded systems.
- Type-Sensitive Operations: Inline functions are preferred over macros because they respect type safety and scope.
Similar Reads
C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
C Language Introduction C is a general-purpose procedural programming language initially developed by Dennis Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming language to write the UNIX operating system.Main features of CWhy Learn C?C is considered mother of all programmin
6 min read
Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are
10 min read
Data Types in C Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.Example:C++int number;The above statement declares a variable with name number that can store integer values.C is a statically type language where
5 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory.To resolve this,
9 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. Balance Factor = left subtree height - right subtree heightFor a Balanced Tree(for every node): -1 ⤠Balance Factor ⤠1Example of an
4 min read
C Arrays An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc. Creating an Array in
7 min read
What is a Neural Network? Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns and enable tasks such as pattern recognition and decision-making.In this article, we will explore the fundamental
12 min read
C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. It is the backbone of low-level memory manipulation in C. Accessing the pointer directly will just give us the address that is stor
9 min read