← Back to C Course | Chapter 14: Additional Topics | Lesson 5 of 9

C Inline Functions

An inline function is a hint that the compiler should paste the function's steps right where it is used, like copying a recipe onto your page instead of flipping to another one. It can save a little time on tiny functions.
Syntax
c
static inline return_type function_name(parameter_type parameter) {
    return expression;
}

What is an Inline Function?

Marking a function inline is a request for the compiler to substitute the function's actual code directly at each call site instead of generating a normal function call, which can eliminate call overhead for small, frequently-called functions like simple getters or one-line calculations.

Example: What is an Inline Function?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
inline int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}

The inline Keyword

To declare an inline function, write the inline keyword before the return type in the function definition; pairing it with static (as static inline) is the conventional way to define small inline helpers in a header without triggering multiple-definition linker errors when that header is included in several files.

Example: The inline Keyword

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
static inline int cube(int n) {
	// Send n * n * n back to the caller
	return n * n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", cube(3));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Inline Functions vs. Macros

Unlike a preprocessor #define macro, which does pure blind text substitution with no awareness of types, an inline function is a real function that the compiler type-checks normally, respects scoping rules for, and can still be stepped through with a debugger — trading a little of a macro's raw flexibility for genuine safety.

Example: Inline Functions vs. Macros

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
static inline int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(1 + 2));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Where to Define Inline Functions

Because the compiler needs to see a function's full body at every point it might be inlined, inline functions intended for use across multiple .c files are typically defined directly inside a shared header, rather than split into a separate declaration-only header plus a .c implementation file.

Example: Where to Define Inline Functions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
static inline int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(2, 3));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Compiler Optimizations

The inline keyword is only ever a hint to the compiler, not a guaranteed instruction — the compiler is free to inline a function that isn't marked inline if it judges that beneficial, and equally free to ignore the inline hint on a function it decides is too large or too complex (such as one containing a loop or recursion) to safely inline.

Example: Compiler Optimizations

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
inline int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(4));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming inline forces inlining, when it is only a hint to the compiler.
  2. Defining an inline function only in a .c file and calling it from another, which can fail to link.
  3. Using a macro instead when type safety is needed.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.