In C programming, function calls introduce a small amount of overhead. When a function is called, the system must save the current state, jump to the function's memory location, execute the code, and then return to the original execution point. For small, frequently called functions, this overhead can become a performance bottleneck.
This is where inline functions come to the rescue.
An inline function is a function that the C compiler attempts to expand directly into the calling code, rather than executing a traditional function call. By doing this, the compiler eliminates the function call overhead, substituting the function's body right where the call is made.
The inline keyword is used to request this behavior from the compiler. Note that it is just a request or a suggestion to the compiler. Modern compilers are smart and may choose to ignore the inline request if the function is too large, recursive, or if optimizing for size rather than speed.
To declare an inline function, simply place the inline keyword before the function's return type.
#include <stdio.h>// Requesting the compiler to inline this function static inline int square(int x) { return x * x; }
int main() { int num = 5; // The compiler may replace the below line with: int result = 5 * 5; int result = square(num); printf("The square of %d is %d\n", num, result); return 0; }
static inline vs inlineIn standard C (specifically C99 and later), you will almost always see inline functions defined as static inline.
static inline: This restricts the function's scope to the current translation unit (the current C file). If the compiler chooses not to inline it, it just creates a normal local function. This prevents "multiple definition" linker errors if you place the inline function inside a header file included by multiple C files.inline (without static): This has complex linkage rules in C and can lead to undefined references if not used carefully with an external definition.Best Practice: Always use static inline when defining inline functions, especially in header files!
Why should you use inline functions?
Inline functions are not a silver bullet. Misusing them can actually degrade performance.
Before the inline keyword was standardized in C99, C programmers heavily relied on preprocessor macros (#define) to achieve inlining.
#include <stdio.h>// Macro approach #define SQUARE_MACRO(x) ((x) * (x))
// Inline function approach static inline int square_inline(int x) { return x * x; }
int main() { int a = 5; // Macro issue: a++ is evaluated twice! ((a++) * (a++)) printf("Macro result: %d\n", SQUARE_MACRO(a++)); int b = 5; // Inline function is safe: b++ is evaluated once, then passed. printf("Inline result: %d\n", square_inline(b++)); return 0; }
i++ to a macro can cause multiple evaluations and terrible bugs. Inline functions evaluate arguments exactly once.What happens if you use the `inline` keyword on a very large function with complex loops?
Why are inline functions generally preferred over preprocessor macros for small calculations?