C#
inline functions
programming
tutorial
coding techniques

How to make inline functions in C

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C, an inline function is a function defined with the inline keyword, which allows the compiler to substitute the function body at the call site when that makes sense. The important detail is that inline is only a hint and also has linkage rules, so using it correctly is as much about declaration style as performance.

Basic Inline Syntax

A small helper function can be declared inline like this:

c
1#include <stdio.h>
2
3static inline int max_int(int a, int b) {
4    return a > b ? a : b;
5}
6
7int main(void) {
8    printf("%d\n", max_int(10, 42));
9    return 0;
10}

For most header-defined helper functions, static inline is the safest pattern. static gives the function internal linkage, which avoids multiple-definition problems when the header is included in several translation units.

Why static inline Is Common in Headers

Suppose you place a small utility in math_utils.h and include that header from many .c files. If the function has external linkage, you need to understand the C standard's inline linkage rules carefully and usually provide one non-inline definition somewhere else.

With static inline, each translation unit gets its own private copy if the compiler decides it needs one, and there is no linker conflict.

c
1/* math_utils.h */
2#ifndef MATH_UTILS_H
3#define MATH_UTILS_H
4
5static inline int square(int value) {
6    return value * value;
7}
8
9#endif
c
1/* app.c */
2#include <stdio.h>
3#include "math_utils.h"
4
5int main(void) {
6    printf("%d\n", square(12));
7    return 0;
8}

That pattern is common in low-level C libraries because it is simple, portable, and avoids macro-style surprises.

Inline Is Not a Guarantee

The compiler may ignore the inline suggestion. Modern compilers decide based on optimization level, function size, debug settings, and code-generation tradeoffs. A function marked inline may still become a normal call, and an unmarked function may still be inlined if the optimizer thinks it is profitable.

That means you should not use inline as a correctness mechanism. It is only a possible performance hint and a way to define short header helpers with the right linkage strategy.

Inline Functions Versus Macros

Inline functions are usually better than function-like macros for type safety and debuggability. A macro such as #define SQUARE(x) ((x) * (x)) can evaluate its argument multiple times. An inline function evaluates each argument once and obeys normal C type checking.

If you need generic behavior across many types, macros still have a role. If you need a small operation on a known type, inline functions are usually the better tool.

Common Pitfalls

  • Using plain inline in a header without understanding linkage rules can lead to undefined references or duplicate definitions.
  • Assuming inline guarantees faster code is incorrect because the compiler may still emit a normal call.
  • Replacing every small function with inline can increase code size and hurt instruction-cache behavior.
  • Confusing inline functions with macros misses the benefits of type checking and single evaluation.
  • Testing only debug builds can mislead you because optimization and inlining behavior differ significantly from release builds.

Summary

  • In C, inline functions use the inline keyword, often together with static in headers.
  • 'static inline is the simplest safe pattern for small helper functions shared across source files.'
  • Inline is a hint, not a promise, so compilers still decide whether substitution is worthwhile.
  • Inline functions are usually safer than macros for fixed-type helpers.
  • Use inline for clarity and structure first, and treat performance gains as workload-dependent.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.