4.7.1.1 Inline Specifier
The inline
function specifier is a recommendation that
the compiler replace calls to the specified function with the function’s body, if
possible. This can increase the execution speed of a program.
Any function with internal linkage (those using a static
specifier) can
be an inline function, but the C99 language standard imposes restrictions on how
functions with external linkage can use the inline
specifier. A file
scope declaration for a function using the inline
function specifier
(but not extern
) is known as an inline definition and merely provides
an alternative to an external definition of that function. You can provide an additional
external definition of that function in others modules, or you could, for example, make
the definition for the function external in the same module by providing an declaration
using extern
.
extern int combine(int x, int y); // make this an external definition
inline int combine(int x, int y) {
return 2 * x - y;
}
The compiler can encode a call to either the inline definition (in-lining the function)
or the external definition, at its discretion. Your code should not make any assumption
about whether in-lining took place. The -Winline
option can be used to
warn you when a function marked in-line could not be substituted, and gives the reason
for the failure.
All function calls to the inline definition will be encoded as if the
call was replaced with the body of the called function. This is performed at the
assembly code level. In-lining will only take place if the optimizers are enabled (level
1 or higher), but you can ask that a function always be in-lined by using the
always_inline
attribute.
If in-lining takes place, this will increase the program’s execution speed, since the call and return sequences associated with the call will be eliminated. Code size can be reduced if the assembly code associated with the body of the in-lined function is very small, but code size can increase if the body of the in-lined function is larger than the call/return sequence it replaces. You should only consider this specifier for functions which generate small amounts of assembly code. Note that the amount of C code in the body of a function is not a good indicator of the size of the assembly code that it generates. As an alternative to in-lined functions, consider using a preprocessor macro.