Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Keywords and Statements

The inline keyword

Reference for the C inline function specifier: what it suggests to the compiler, the static inline idiom for headers, and the C99 external definition rules.

inline is a function specifier that suggests the compiler substitute the function body at the call site rather than emit a call. It is only a hint: the compiler may inline functions without the keyword and may ignore it on functions with it. What the keyword really changes are the linkage rules, which allow a function definition to appear in a header without causing multiple-definition errors.

The idiom to remember is static inline in a header. Each file that includes it gets a private copy, the compiler can inline it freely, and there is nothing to link.

C
static inline int max_int(int a, int b) {
    return a > b ? a : b;
}

Try it

C static inline helpers

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.

C inline with an external definition

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.

Syntax

C
inline return-type name(parameters) { body }
static inline return-type name(parameters) { body }
extern inline return-type name(parameters);   /* emits the external definition */
Form Meaning
static inline Internal linkage. Safe in headers. The recommended form.
inline (plain, C99+) An inline definition: it does not by itself provide an external definition. Another file must provide one.
extern inline Declaration in a file that already sees the inline definition: that file then contains the external definition.

Errors: a plain inline function that the compiler chooses not to inline, with no external definition anywhere, causes a linker error ("undefined reference"). An inline function with external linkage may not define a modifiable static local variable, nor refer to a file-scope static object.

Usage notes

  • inline is not #define. Arguments are type-checked and evaluated once, and the function can be debugged and have its address taken.
  • Prefer static inline in headers; use plain inline only when you deliberately want one shared external copy.
  • C and C++ differ. In C++ a plain inline function is fine in a header with no extra definition. Code meant for both languages should use static inline.
  • Size matters. Inlining large functions bloats the code and can slow programs by hurting the instruction cache; leave this decision to -O2 unless profiling shows otherwise.
  • Forcing: __attribute__((always_inline)) (GNU) overrides the hint, noinline prevents it. Neither is standard C.
  • Recursion: a recursive function cannot be fully inlined.
  • Compile flags: at -O0 GCC does not inline ordinary inline functions, so such a function without an external definition fails to link in debug builds.