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
time() function in C
The time() function is defined in time.h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.Syntax: time_t time( time_t *seco
1 min read
C Library Functions
The Standard Function Library in C is a huge library of sub-libraries, each of which contains the code for several functions. In order to make use of these libraries, link each library in the broader library through the use of header files. The actual definitions of these functions are stored in sep
9 min read
ungetc() in C/C++
The ungetc() function takes a single character and shoves it back onto an input stream. It is the opposite of the getc() function, which reads a single character from an input stream. Also, ungetc() is an input function, not an output function. Syntax: int ungetc(int char, FILE *stream) Parameters:
3 min read
Concatenating Two Strings in C
Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C.The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example:C#include <stdio.h> #i
2 min read
Program and syntax for iscntrl(int c) function in C
In C, iscntrl() is a predefined function used for string and character handling. ctype is the header file required for character functions. A control character is one that is not a printable character i.e, it does not occupy a printing position on a display. This function is used to check if the arg
4 min read
Interesting Facts in C Programming | Set 2
Below are some more interesting facts about C programming: 1. Macros can have unbalanced braces: When we use #define for a constant, the preprocessor produces a C program where the defined constant is searched and matching tokens are replaced with the given expression. Example:C #include <stdio.h
2 min read
sin() in C
The sin() function in C is a standard library function to find the sine value of the radian angle. It is used to evaluate the trigonometric sine function of an angle. It takes the radian angle as input and returns the sin of that angle. It is defined inside <math.h> header file. Syntax of sin(
1 min read
How to Create a Typedef for a Function Pointer in C?
In C, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. The typedef is a keyword used to create an alias or alternative name for the existing data types. In this article, we will learn how to create a typedef for a function
2 min read
How to Create a Dynamic Library in C?
In C, a dynamic library is a library that is loaded at runtime rather than compile time. We can create our own dynamic libraries and use them in our programs. In this article, we will learn how to create a dynamic library in C. Example: Input:Hello, geeksforgeek!Output:Hello, geeksforgeek!Creating a
2 min read
C Multiple Choice Questions
C is the most popular programming language developed by Dennis Ritchie at the Bell Laboratories in 1972 to develop the UNIX operating systems. It is a general-purpose and procedural programming language. It is faster than the languages like Java and Python. C is very versatile it can be used in both
4 min read