C Inline Functions
Calling a function has a small cost: arguments are set up, the program jumps to the function, and then jumps back. For a big function this cost is negligible, but for a one-line helper called millions of times in a tight loop it can add up. The inline keyword, introduced in C99, suggests to the compiler that it may copy the function's body directly into each call site instead of performing a real call.
Declaring an inline function
The most practical form in C is static inline:
#include <stdio.h>
static inline int square(int x) {
return x * x;
}
static inline int clamp(int value, int low, int high) {
if (value < low) return low;
if (value > high) return high;
return value;
}
int main(void) {
printf("square(9) = %d\n", square(9));
printf("clamp(150, 0, 100) = %d\n", clamp(150, 0, 100));
printf("clamp(-5, 0, 100) = %d\n", clamp(-5, 0, 100));
printf("clamp(42, 0, 100) = %d\n", clamp(42, 0, 100));
return 0;
}square(9) = 81
clamp(150, 0, 100) = 100
clamp(-5, 0, 100) = 0
clamp(42, 0, 100) = 42
From the caller's point of view, an inline function is called and behaves exactly like any other function: arguments are evaluated once, types are checked, and the result is returned.
inline is only a hint
inline does not force anything. The compiler decides whether to inline a call based on its optimisation settings, and modern compilers inline small static functions on their own even without the keyword. Likewise, they may refuse to inline a function marked inline if it is large or recursive.
What inline does |
What it does not do |
|---|---|
| allows the body to be substituted at the call site | guarantee that substitution happens |
| changes linkage rules (see below) | make every program faster |
| lets you place small function definitions in header files | change how arguments are passed |
Why static inline?
C's rules for plain inline (without static) are surprisingly tricky: an inline definition alone does not provide an external definition, so if the compiler decides not to inline a call, the linker may fail with "undefined reference". Adding static gives each file its own private copy and avoids the problem. That is why static inline is the common idiom, especially for small helpers in header files:
// mathutil.h
#ifndef MATHUTIL_H
#define MATHUTIL_H
static inline double lerp(double a, double b, double t) {
return a + (b - a) * t;
}
#endifEvery .c file that includes this header can call lerp, and the compiler is free to inline it everywhere.
Inline functions vs. macros
Before C99, programmers used function-like macros for tiny helpers. Macros are pure text replacement, which causes surprises that inline functions avoid:
#include <stdio.h>
#define SQUARE_MACRO(x) ((x) * (x))
static inline int square_fn(int x) {
return x * x;
}
static int calls = 0;
static int next_value(void) { // returns 1, 2, 3, ... and counts its calls
calls++;
return calls;
}
int main(void) {
int m = SQUARE_MACRO(next_value()); // expands to ((next_value()) * (next_value()))
printf("macro: result %d, next_value called %d times\n", m, calls);
calls = 0;
int f = square_fn(next_value()); // argument evaluated exactly once
printf("function: result %d, next_value called %d time\n", f, calls);
return 0;
}macro: result 2, next_value called 2 times
function: result 1, next_value called 1 time
The macro pasted the argument text twice, so next_value() ran twice and the "square" became 1 × 2 = 2. With side effects such as x++ it is even worse: SQUARE_MACRO(a++) modifies a twice in one expression, which is undefined behaviour. The inline function evaluates its argument once and behaves like a normal call.
| Function-like macro | static inline function |
|
|---|---|---|
| Type checking | none | full |
| Arguments evaluated | possibly several times | exactly once |
| Debugging | hard (text substitution) | normal |
| Works with any type | yes (generic) | no, one set of types |
| Can be stepped into in a debugger | no | yes (in debug builds) |
A practical example
Small helpers like these are typical candidates for static inline. They make code more readable at zero runtime cost:
#include <stdio.h>
#include <stdbool.h>
static inline bool is_vowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
static inline int max_int(int a, int b) {
return a > b ? a : b;
}
int main(void) {
const char *word = "encyclopedia";
int vowels = 0, longest_run = 0, run = 0;
for (int i = 0; word[i] != '\0'; i++) {
if (is_vowel(word[i])) {
vowels++;
run = 0;
} else {
run++;
longest_run = max_int(longest_run, run);
}
}
printf("%s: %d vowels, longest consonant run %d\n", word, vowels, longest_run);
return 0;
}encyclopedia: 5 vowels, longest consonant run 5
Tip: Don't sprinkle
inlineeverywhere hoping for speed. Write clear functions, compile with optimisation (-O2), and only reach forinlinefor tiny helpers — typically ones defined in header files.
Test yourself
What does the inline keyword do?
- Forces the compiler to inline every call
- Suggests that calls may be replaced by the function body
- Makes the function a macro
- Makes the function run in parallel
Why is static inline the usual form for helpers in header files?
- It is faster than inline
- It gives each file its own copy and avoids linker errors
- It makes the function global
- It is required by the C standard
With #define DOUBLE(x) ((x) + (x)), why is DOUBLE(i++) a problem?
- Macros cannot take arguments
- i++ is evaluated twice
- It does not compile
- It always returns 0