Skip to main content

Lesson 5 of 8

inline Functions & Macros - When to Use Which

`inline` is a hint to the compiler to expand the function body at the call site, eliminating the function call overhead. It does NOT guarantee inlining - the compiler decides. `static inline` in header files is the standard production pattern for small utility functions that should be candidates for inlining across translation units.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

inline vs Macro - Trade-offs & Correct Usage

inline vs Macro - Trade-offs & Correct Usage
/* utils.h - header with inline utilities */
#ifndef UTILS_H
#define UTILS_H

#include <stddef.h>   /* size_t */

/* ── static inline - the correct production pattern ────────
   'static': one copy per translation unit (no multiple-definition error)
   'inline': hint to expand at call site (no overhead)
   Result: type-safe, debuggable, and fast.                   */

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

static inline int min_int(int a, int b) {
    return a < b ? a : b;
}

static inline int clamp(int val, int lo, int hi) {
    return max_int(lo, min_int(val, hi));
}

/* ── vs #define - why inline is better ──────────────────────
   #define MAX(a,b) a > b ? a : b        ← NO parens: wrong
   #define MAX(a,b) ((a) > (b) ? (a) : (b))  ← double-eval!
   MAX(x++, y)  expands to ((x++) > (y) ? (x++) : (y))
   x is incremented TWICE - undefined behaviour!

   With static inline:
   max_int(x++, y) - x++ evaluated ONCE, safe.               */

/* ── abs macro from <stdlib.h> vs custom inline ─────────── */
static inline int abs_int(int n) {
    return n < 0 ? -n : n;
}

/* ── Swap macro - one case where macro wins ──────────────── */
/* For a type-generic swap, macros are unavoidable in C89/C99: */
#define SWAP(type, a, b)  do { type _tmp = (a); (a) = (b); (b) = _tmp; } while (0)
/* do { } while (0) makes it a single statement - required for
   use inside if/else without braces:
   if (cond) SWAP(int, x, y);   → safe                        */

#endif /* UTILS_H */

/* ── Example usage (main.c) ──────────────────────────────── */
/*
#include <stdio.h>
#include "utils.h"

int main(void) {
    printf("max(3,7) = %d
", max_int(3, 7));       // 7
    printf("clamp(15,0,10) = %d
", clamp(15,0,10)); // 10

    int x = 5, y = 3;
    SWAP(int, x, y);
    printf("x=%d y=%d
", x, y);   // x=3 y=5

    return 0;
}
*/

Common Mistakes

  • Putting a non-static inline function in a header - if multiple `.c` files include the header, each gets a definition. Without `static`, this causes multiple-definition linker errors.
  • Relying on inline to always inline - the compiler may choose not to inline (too complex, too large). For guaranteed inlining use `__attribute__((always_inline))` (GCC/Clang extension) or just trust the compiler.
  • Using macros with side-effect arguments - `MAX(x++, y)` evaluates `x++` twice. This is a well-known macro trap. Prefer `static inline` functions whenever arguments may have side effects.

Tip

Tip

Practice inline Functions Macros When to Use Which in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

inline provides compiler-checked type safety while avoiding function call overhead

Practice Task

Note

Practice Task - (1) Write a working example of inline Functions Macros When to Use Which from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with inline Functions Macros When to Use Which is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • `inline` is a hint to the compiler to expand the function body at the call site, eliminating the function call overhead.
  • Putting a non-static inline function in a header - if multiple `.c` files include the header, each gets a definition. Without `static`, this causes multiple-definition linker errors.
  • Relying on inline to always inline - the compiler may choose not to inline (too complex, too large). For guaranteed inlining use `__attribute__((always_inline))` (GCC/Clang extension) or just trust the compiler.
  • Using macros with side-effect arguments - `MAX(x++, y)` evaluates `x++` twice. This is a well-known macro trap. Prefer `static inline` functions whenever arguments may have side effects.

Topics in This Module

0/8 done