5.28 Inline functions in C99 mode

The rules for C99 inline functions with external linkage differ from those of C++.

C99 distinguishes between inline definitions and external definitions. Within a given translation unit where the inline function is defined, if the inline function is always declared with inline and never with extern, it is an inline definition. Otherwise, it is an external definition. These inline definitions do not generate out-of-line copies, even when --no_inline is used.

Each use of an inline function might be inlined using a definition from the same translation unit (that might be an inline definition or an external definition), or it might become a call to an external definition. If an inline function is used, it must have exactly one external definition in some translation unit. This is the same rule that applies to using any external function. In practise, if all uses of an inline function are inlined, no error occurs if the external definition is missing. If you use --no_inline, only external definitions are used.

Typically, you put inline functions with external linkage into header files as inline definitions, using inline, and not using extern. There is also an external definition in one source file. For example:

/* example_header.h */
inline int my_function (int i)
{
   return i + 42; // inline definition
}
/* file1.c */
#include "example_header.h"
   ... // uses of my_function()
/* file2.c */
#include "example_header.h"
   ... // uses of my_function()
/* myfile.c */
#include "example_header.h"
extern inline int my_function(int); // causes external definition.

This is the same strategy that is typically used for C++, but in C++ there is no special external definition, and no requirement for it.

The definitions of inline functions can be different in different translation units. However, in typical use, as in the above example, they are identical.

When compiling with --multifile, calls in one translation unit might be inlined using the external definition in another translation unit.

C99 places some restrictions on inline definitions. They cannot define modifiable local static objects. They cannot reference identifiers with static linkage.

In C99 mode, as with all other modes, the effects of __inline and inline are identical.

Inline functions with static linkage have the same behavior in C99 as in C++.

Related concepts
5.20 Inline functions
5.21 Compiler decisions on function inlining
5.22 Automatic function inlining and static functions
5.23 Inline functions and removal of unused out-of-line functions at link time
5.24 Automatic function inlining and multifile compilation
5.26 Compiler modes and inline functions
5.27 Inline functions in C++ and C90 mode
5.29 Inline functions and debugging
Related reference
5.25 Restriction on overriding compiler decisions about function inlining
8.106 --inline, --no_inline
8.133 --multifile, --no_multifile
10.8 __inline
Non-ConfidentialPDF file icon PDF versionARM DUI0472J
Copyright © 2010-2013 ARM. All rights reserved.