Nearby lessons
84 of 124C - Inline Functions
Inline functions ask the compiler to paste a function's body at the call site instead of making a real call. Learn the inline keyword, how it differs from a macro, and why static inline is the form you should almost always use.
The inline Keyword
Add inline before the return type. The compiler may then replace each call with a copy of the body, skipping the call overhead:
What "Inlining" Actually Means
Why static inline
Plain inline in C has awkward linkage rules: if the compiler chooses not to inline a call, it needs an external definition to call — and there may not be one. static inline gives every translation unit its own copy and sidesteps the problem entirely:
inline Is Only a Hint
The compiler is free to ignore inline, and free to inline functions you never marked. Optimisation level matters more than the keyword:
Inline Functions vs Macros
Both avoid call overhead, but only one is type-checked and evaluates its arguments once:
static inline function | #define macro | |
|---|---|---|
| Handled by | The compiler | The preprocessor |
| Type checking | Yes | No |
| Arguments evaluated | Once | Every time they appear |
| Debuggable | Yes | Barely |
| Needs defensive parentheses | No | Yes |
| Verdict | Preferred | Only for compile-time tricks |
The Double-Evaluation Trap
The macro expands to ((next()) * (next())) — the argument text is substituted twice, so the side effect happens twice and the answer is wrong. The inline function receives one already-evaluated argument, so it cannot misbehave this way.
i++, getchar(), f() — is duplicated. An inline function evaluates each argument exactly once, like every other function call.Good Candidates for Inlining
Small accessors, converters and predicates called in tight loops:
When Not to Inline
Inlining copies code. Do it to a large function called from fifty places and your binary balloons — which can make the program slower by pushing code out of the CPU's instruction cache.
| Inline it | Leave it alone |
|---|---|
| One or two lines | Dozens of lines |
| Called in a hot loop | Called once at startup |
| Simple arithmetic or a comparison | Contains loops or heavy branching |
| Accessors and predicates | Recursive functions |
| Header-only helpers | Anything that does I/O |
Common Mistakes
- Plain
inlinein a header — risksundefined referenceat link time. Usestatic inline. - Assuming the keyword forces inlining — it is a hint. Use
__attribute__((always_inline))on gcc if you truly must. - Inlining large functions — bigger binary, worse cache behaviour.
- Trying to inline a recursive function — the compiler cannot expand it fully.
- Optimising before measuring — profile first; inlining rarely turns out to be the bottleneck.
inline does nothing at -O0, which is where most beginners compile. Modern compilers at -O2 inline aggressively on their own, guided by measurement rather than keywords. Write clear small functions and let the optimiser work.- inline is a hint, not a command — the compiler decides.
- Use static inline in headers to avoid linker errors.
- Inline functions are type-checked; macros are not.
- Inlining suits small, frequently called functions.
- Inlining large functions makes the program bigger and often slower.