C Inline Functions
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Assuming
inlineforces inlining, when it is only a hint to the compiler. - Defining an inline function only in a
.cfile and calling it from another, which can fail to link. - Using a macro instead when type safety is needed.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: