The inline function specifier

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. Instead of transferring control to and from the function code segment, a modified copy of the function body may be substituted directly for the function call. In this way, the performance overhead of a function call is avoided.

C only Any function, with the exception of main, can be declared or defined as inline with the inline function specifier. Static local variables are not allowed to be defined within the body of an inline function.

C++ C++ functions implemented inside of a class declaration are automatically defined inline. Regular C++ functions and member functions declared outside of a class declaration, with the exception of main, can be declared or defined as inline with the inline function specifier. Static locals and string literals defined within the body of an inline function are treated as the same object across translation units; see Linkage of inline functions for details.

The following code fragment shows an inline function definition:

inline int add(int i, int j) { return i + j; }

The use of the inline specifier does not change the meaning of the function. However, the inline expansion of a function may not preserve the order of evaluation of the actual arguments.

The most efficient way to code an inline function is to place the inline function definition in a header file, and then include the header in any file containing a call to the function which you would like to inline.

Note:
The inline qualifier is represented by the following keywords:

Related information

Linkage of inline functions

C only

In C, inline functions are treated by default as having static linkage; that is, they are only visible within a single translation unit. Therefore, in the following example, even though function foo is defined in exactly the same way, foo in file a.c and foo in file b.c are treated as separate functions: two function bodies are generated, and assigned two different addresses in memory:

// a.c

#include <stdio.h>

inline int foo(){
return 3;
}

void g() {
printf("foo called from g: return value = %d, address = %p\n", foo(), &foo);
}


// b.c

#include <stdio.h>

inline int foo(){
return 3;
}

void g();

int main() {
printf("foo called from main: return value = %d, address = %p\n", foo(), &foo);
g();
}

The output from the compiled program is:

foo called from main: return value = 3, address = 0x10000580 
foo called from g: return value = 3, address = 0x10000500

Since inline functions are treated as having internal linkage, an inline function definition can co-exist with a regular, external definition of a function with the same name in another translation unit. However, when you call the function from the file containing the inline definition, the compiler may choose either the inline version defined in the same file or the external version defined in another file for the call; your program should not rely on the inline version being called. In the following example, the call to foo from function g could return either 6 or 3:

// a.c

#include <stdio.h>

inline int foo(){
return 6;
}

void g() {
printf("foo called from g: return value = %d\n", foo());
}


// b.c

#include <stdio.h>

int foo(){
return 3;
}

void g();

int main() {
printf("foo called from main: return value = %d\n", foo());
g();
}
Similarly, if you define a function as extern inline, or redeclare an inline function as extern, the function simply becomes a regular, external function and is not inlined.

IBM extension If you specify the __inline__ keyword, with the trailing underscores, the compiler uses the GNU C semantics for inline functions. In contrast to the C99 semantics, a function defined as __inline__ provides an external definition only; a function defined as static __inline__ provides an inline definition with internal linkage (as in C99); and a function defined as extern __inline__, when compiled with optimization enabled, allows the co-existence of an inline and external definition of the same function. For more information on the GNU C implementation of inline functions, see the GCC documentation, available at http://gcc.gnu.org/onlinedocs/.

End of C only
C++ only

You must define an inline function in exactly the same way in each translation unit in which the function is used or called. Furthermore, if a function is defined as inline, but never used or called within the same translation unit, it is discarded by the compiler (unless you compile with the -qkeepinlines option).

Nevertheless, in C++, inline functions are treated by default as having external linkage, meaning that the program behaves as if there is only one copy of the function. The function will have the same address in all translation units and each translation unit will share any static locals and string literals. Therefore, compiling the previous example gives the following output:

foo called from main: return value = 3, address = 0x10000580 
foo called from g: return value = 3, address = 0x10000580
Redefining an inline function with the same name but with a different function body is illegal; however, the compiler does not flag this as an error, but simply generates a function body for the version defined in the first file entered on the compilation command line, and discards the others.

Therefore, the following example, in which inline function foo is defined differently in two different files, may not produce the expected results:

// a.C

#include <stdio.h>

inline int foo(){
return 6;
}

void g() {
printf("foo called from g: return value = %d, address = %p\n", foo(), &foo);
}


// b.C

#include <stdio.h>

inline int foo(){
return 3;
}

void g();

int main() {
printf("foo called from main: return value = %d, address = %p\n", foo(), &foo);
g();
}

When compiled with the command xlC a.C b.C, the output is:

foo called from main: return value = 6, address = 0x10001640
foo called from g: return value = 6, address = 0x10001640

The call to foo from main does not use the inline definition provided in b.C, but rather calls foo as a regular external function defined in a.C. It is your responsibility to ensure that inline function definitions with the same name match exactly across translation units, to avoid unexpected results.

Because inline functions are treated as having external linkage, any static local variables or string literals that are defined within the body of an inline function are treated as the same object across translation units. The following example demonstrates this:

// a.C

#include <stdio.h>

inline int foo(){
static int x = 23;
printf("address of x = %p\n", &x);
x++;
return x;
}

void g() {
printf("foo called from g: return value = %d\n", foo());
}


// b.C

#include <stdio.h>

inline int foo()
{
static int x=23;
printf("address of x = %p\n", &x);
x++;
return x; 
}

void g();

int main() {
printf("foo called from main: return value = %d\n", foo());
g();
}

The output of this program shows that x in both definitions of foo is indeed the same object:

address of x = 0x10011d5c
foo called from main: return value = 24
address of x = 0x10011d5c
foo called from g: return value = 25

If you want to ensure that each instance of function defined as inline is treated as a separate function, you can use the static specifier in the function definition in each translation unit, or compile with the -qstaticinline option. Note, however, that static inline functions are removed from name lookup during template instantiation, and are not found.

Related information

End of C++ only