C Reference ยท Functions and Scope
Inline functions in C
Reference for inline functions in C99 and later: what inline means, static inline in headers, the extern inline definition rule, and inline versus macros.
The inline function specifier is a hint to the compiler that calls to the function should be as fast as possible, usually by pasting the body into the caller. The compiler is free to ignore it, and it may also inline functions that are not marked. What inline reliably changes is linkage and the one-definition rules, which makes it the right tool for small functions defined in headers.
C99 inline differs from C++ inline and from the older GNU C89 inline. In standard C, an inline function without static does not by itself provide an external definition, which surprises many people with link errors.
static inline int square(int x) { return x * x; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
A header that defines one inline function the C99 way looks like this (read-only, it needs two files):
/* mathutil.h */ inline int cube(int x) { return x * x * x; } /* inline definition: no external symbol */ /* mathutil.c */ #include "mathutil.h" extern inline int cube(int x); /* the one external definition */
Syntax
inline return-type name(parameters) { body } /* inline definition */ static inline return-type name(parameters) { body } /* internal linkage; the usual form */ extern inline return-type name(parameters); /* forces an external definition in this file */
| Form | Meaning |
|---|---|
static inline |
Each translation unit has its own private copy. Safe in headers; no linker symbol is needed. |
inline (all declarations in the file use inline, none use extern) |
An inline definition: callable, but it does not define an external symbol. The compiler may call an external definition from another file instead of inlining. |
extern inline or a plain non-inline declaration |
Makes this the external definition that other files' inline definitions may fall back on. |
Rules: an inline function with external linkage must not define a non-constant static variable or reference a static object with internal linkage, because different translation units could see different copies. inline cannot be used on main.
Errors / undefined behavior: an inline function declared inline everywhere in a file, if the compiler decides not to inline it and no other file provides the external definition, fails at link time with an undefined reference. Defining differing bodies for the same inline function in several files is undefined.
Usage notes
- Use
static inlinefor header functions. It avoids all linkage questions and is the pattern in most codebases. inlineis a hint. At-O0GCC does not inline anything; at-O2it inlines by its own heuristics. Force it with attributes (__attribute__((always_inline))) only when measurement proves it.- Benefits: no call overhead and better optimization across the call. Costs: larger code, more instruction-cache pressure, longer compile times. Small, hot functions benefit; big ones do not.
- Inline versus macros. A function checks argument types, evaluates arguments once, and can be debugged. Macros still win for type-generic code, constants and token pasting. See Macros vs. functions.
- Pointers. Taking the address of an inline function forces an out-of-line copy. In a header, a
static inlinefunction's address differs per translation unit. - Compatibility. GNU C89
inline(used by GCC with-std=gnu89) has opposite rules from C99. Always compile in C99 or later (the default of modern GCC is C17/C23). - Not a promise to the optimizer about performance: modern compilers inline unmarked
staticfunctions called once. - Debug builds keep the call, so stepping into inline functions in a debugger usually works.