liveBook · Manning

Inlining

As explained in Modern C, Third Edition

Inlining is a compiler transformation that replaces a function call with the called function's implementation.

Inlining replaces a function call with the called function’s implementation. Removing the call can avoid stack setup, control-flow transfer, cache disruption, and copying of struct return values. section 16.1

A definition visible to the compiler

When caller and callee occur in the same translation unit, a compiler may inline the callee. Seeing its instructions in the caller’s context can expose dead branches, already-known computations, and restricted return values for further optimization. section 16.1

Inlining depends on more than declaring a function:

  • A traditional C compiler needs the function definition, not merely its declaration. section 16.1
  • Definitions placed in headers can make them visible to multiple translation units. section 16.1
  • Inline definitions require care because preprocessing can replace function argument names and local variables. section 16.1
 // Inline definition in a header file.
 // Function argument names and local variables are visible
 // to the preprocessor and must be handled with care.
 inline
 toto* toto_init(toto* toto_x){
   if (toto_x) {
     *toto_x = (toto){ };
   }
   return toto_x;
 }

Inspect a header-style inline definition and its interaction with preprocessing. section 16.1

How Modern C, Third Edition covers it

How deep it goes
Explains replacement semantics, call overhead, optimization opportunities, and definition-visibility strategies across translation units.
Examples
Uses a small inline initializer, a header definition, and a separate translation-unit declaration.
What it assumes
Assumes function declarations, definitions, translation units, headers, and basic compiler optimization terminology.
What you can do after
Evaluate when visible C definitions can eliminate call overhead and enable context-sensitive optimization.
How it connects
Connects function calls with translation units, compiler optimization, function definitions, declarations, header files, and preprocessing.

Also explained in

Compare how 4 books explain inlining →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.