What is the use of the `inline` keyword in C?
Asked Answered
I

6

88

I read several questions in stackoverflow about inline in C but still am not clear about it.

  1. static inline void f(void) {} has no practical difference with static void f(void) {}.
  2. inline void f(void) {} in C doesn't work as the C++ way. How does it work in C?
  3. What actually does extern inline void f(void); do?

I never really found a use of the inline keyword in my C programs, and when I see this keyword in other people's code, it's almost always static inline, in which I see no difference with just static.

Insulation answered 29/6, 2015 at 5:8 Comment(5)
Both comments are misleading. Of course inline is of use in C, and the link leads to a C++ question which just adds to confusion.Memorize
en.cppreference.com/w/c/language/inlineNasho
Is "inline" without "static" or "extern" ever useful in C99? shows the actual syntax necessary in the .h and the extern inline declaration in exactly one .c to instantiate a non-inline definition and make a working C program with a non-static inline function defined in a header and thus able to actually inline. In C, should inline functions in headers be externed in the .c file? is also about that, with a concise answer.Reposition
WRT "static inline... has no practical difference with static...": maybe you saying that they act the same syntactically -- the visibility of the function is the same for both. And (even if the compiler does inline code), the resulting code behavior the same either way. But, (if the compiler does inline), then the resulting code runs faster since the function call was optimized out. It's very possible that this speed enhancement is so small it's not perceivable, but it's there. If you use inline wisely it can have value.Smasher
I use 'static inline' with some regularity in C. It allows me to factor code within a file (translation unit) into functions without worrying that small functions will kill performance due to call overhead. IOW, I use inline on smallish static functions. Other developers might inline the source code instead of defining a small, static function.Smasher
I
41

Note: when I talk about .c files and .h files in this answer, I assume you have laid out your code correctly, i.e. .c files only include .h files. The distinction is that a .h file may be included in multiple translation units.

static inline void f(void) {} has no practical difference with static void f(void) {}.

In ISO C, this is correct. They are identical in behaviour (assuming you don't re-declare them differently in the same TU of course!) the only practical effect may be to cause the compiler to optimize differently.

inline void f(void) {} in C doesn't work as the C++ way. How does it work in C? What actually does extern inline void f(void); do?

This is explained by this answer and also this thread.

In ISO C and C++, you can freely use inline void f(void) {} in header files -- although for different reasons!

In ISO C, it does not provide an external definition at all. In ISO C++ it does provide an external definition; however C++ has an additional rule (which C doesn't), that if there are multiple external definitions of an inline function, then the compiler sorts it out and picks one of them.

extern inline void f(void); in a .c file in ISO C is meant to be paired with the use of inline void f(void) {} in header files. It causes the external definition of the function to be emitted in that translation unit. If you don't do this then there is no external definition, and so you may get a link error (it is unspecified whether any particular call of f links to the external definition or not).

In other words, in ISO C you can manually select where the external definition goes; or suppress external definition entirely by using static inline everywhere; but in ISO C++ the compiler chooses if and where an external definition would go.

In GNU C, things are different (more on this below).

To complicate things further, GNU C++ allows you to write static inline an extern inline in C++ code... I wouldn't like to guess on what that does exactly

I never really found a use of the inline keyword in my C programs, and when I see this keyword in other people's code, it's almost always static inline

Many coders don't know what they're doing and just put together something that appears to work. Another factor here is that the code you're looking at might have been written for GNU C, not ISO C.

In GNU C, plain inline behaves differently to ISO C. It actually emits an externally visible definition, so having a .h file with a plain inline function included from two translation units causes undefined behaviour.

So if the coder wants to supply the inline optimization hint in GNU C, then static inline is required. Since static inline works in both ISO C and GNU C, it's natural that people ended up settling for that and seeing that it appeared to work without giving errors.

, in which I see no difference with just static.

The difference is just in the intent to provide a speed-over-size optimization hint to the compiler. With modern compilers this is superfluous.

Immoderate answered 29/6, 2015 at 6:9 Comment(8)
"extern inline void f(void); in a .c file is meant to be paired with the use of static inline void f(void) {} in header files". Shouldn't you remove the static?Insulation
I've just read the recent C standard draft which states "If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit." Does this mean that the extern declaration is unnecessary if I meet the requirements of an inline definition?Insulation
@xiver77 you're right about removing the static; I re-read the standard just now and realized one of the other canonical answers had misread it. "It is unspecified whether a call to the function uses the inline definition or the external definition. " only applies to calls to inline functions with external linkage, so calls to a static inline cannot call the externally-defined version. (This actually seems to me to provide a barrier to the compiler from doing whole-program optimization)Immoderate
I'm not sure what you mean in your latest question though. In a TU, inline void f(void) {} without extern void f(void); means that it is both an inline definition and a function with external linkage, so calls to the function might be linked to the external definition instead of this TU's inline definition.Immoderate
I now understand. So without the extern declaration, I may not get linker errors if the compiler uses the inline definition, but I do get linker errors if the compiler chooses the external definition. I just have tested with gcc with and without optimizations. Without optimization I get a linker error for not having an external definition.Insulation
@xiver77 yes that sounds right , as in the example in this questionImmoderate
"They ['static inline' and 'static'] are identical in behavior" is confusing since although they are have the scoping/visibility (might call that syntactic behavior) and that the resulting runtime behavior should be logically the same, that misses the point of inline. The resulting code is different (if the compiler does actually inline the code). Runtime behavior will be different since it will run faster which is the whole point.Smasher
I don't, but some consider including a .c file to be 'correct'.Smasher
R
77

A C program can be optimized with two goals: For reducing the Code size or for getting the best performance (time). Usually, these are two opposite ends on the line. For reducing the code size, the program is optimized in such a way that it compromises the performance to an extent. And with compiler set to optimize for the performance in terms of execution time, the generated binary is usually of higher code size. Since it can be requirement-dependent, it is usually a trade-off between what fits the requirement.

Inline functions are typically seen as a means of getting higher performance meaning, reduced execution times.

inline functions:

gcc.gnu.org says,

By declaring a function inline, you can direct GCC to make calls to that function faster. One way GCC can achieve this is to integrate that function's code into the code for its callers. This makes execution faster by eliminating the function-call overhead; in addition, if any of the actual argument values are constant, their known values may permit simplifications at compile time so that not all of the inline function's code needs to be included. The effect on code size is less predictable; object code may be larger or smaller with function inlining, depending on the particular case.

So, it tells the compiler to build the function into the code where it is used with the intention of improving execution time.

If you declare Small functions like setting/clearing a flag or some bit toggle which are performed repeatedly, inline, it can make a big performance difference with respect to time, but at the cost of code size.


non-static inline and Static inline

Again referring to gcc.gnu.org,

When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-static inline function is always compiled on its own in the usual fashion.


extern inline?

Again, gcc.gnu.org, says it all:

If you specify both inline and extern in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it.

This combination of inline and extern has almost the effect of a macro. The way to use it is to put a function definition in a header file with these keywords, and put another copy of the definition (lacking inline and extern) in a library file. The definition in the header file causes most calls to the function to be inlined. If any uses of the function remain, they refer to the single copy in the library.


To sum it up:

  1. For inline void f(void){}, inline definition is only valid in the current translation unit.
  2. For static inline void f(void) {} Since the storage class is static, the identifier has internal linkage and the inline definition is invisible in other translation units.
  3. For extern inline void f(void); Since the storage class is extern, the identifier has external linkage and the inline definition also provides the external definition.
Rosannarosanne answered 29/6, 2015 at 5:47 Comment(1)
The last quote about extern inline is only true for C90. That is also mentioned in the GCC documentation. See this compiler explorer link for a comparison: godbolt.org/z/7cdnMfnW5 . For C90, no assembler code is generated from a extern inline function, for other C standard the is code generated with external linkage, see assembler directive .globl extern_inline_fn.Rennet
I
41

Note: when I talk about .c files and .h files in this answer, I assume you have laid out your code correctly, i.e. .c files only include .h files. The distinction is that a .h file may be included in multiple translation units.

static inline void f(void) {} has no practical difference with static void f(void) {}.

In ISO C, this is correct. They are identical in behaviour (assuming you don't re-declare them differently in the same TU of course!) the only practical effect may be to cause the compiler to optimize differently.

inline void f(void) {} in C doesn't work as the C++ way. How does it work in C? What actually does extern inline void f(void); do?

This is explained by this answer and also this thread.

In ISO C and C++, you can freely use inline void f(void) {} in header files -- although for different reasons!

In ISO C, it does not provide an external definition at all. In ISO C++ it does provide an external definition; however C++ has an additional rule (which C doesn't), that if there are multiple external definitions of an inline function, then the compiler sorts it out and picks one of them.

extern inline void f(void); in a .c file in ISO C is meant to be paired with the use of inline void f(void) {} in header files. It causes the external definition of the function to be emitted in that translation unit. If you don't do this then there is no external definition, and so you may get a link error (it is unspecified whether any particular call of f links to the external definition or not).

In other words, in ISO C you can manually select where the external definition goes; or suppress external definition entirely by using static inline everywhere; but in ISO C++ the compiler chooses if and where an external definition would go.

In GNU C, things are different (more on this below).

To complicate things further, GNU C++ allows you to write static inline an extern inline in C++ code... I wouldn't like to guess on what that does exactly

I never really found a use of the inline keyword in my C programs, and when I see this keyword in other people's code, it's almost always static inline

Many coders don't know what they're doing and just put together something that appears to work. Another factor here is that the code you're looking at might have been written for GNU C, not ISO C.

In GNU C, plain inline behaves differently to ISO C. It actually emits an externally visible definition, so having a .h file with a plain inline function included from two translation units causes undefined behaviour.

So if the coder wants to supply the inline optimization hint in GNU C, then static inline is required. Since static inline works in both ISO C and GNU C, it's natural that people ended up settling for that and seeing that it appeared to work without giving errors.

, in which I see no difference with just static.

The difference is just in the intent to provide a speed-over-size optimization hint to the compiler. With modern compilers this is superfluous.

Immoderate answered 29/6, 2015 at 6:9 Comment(8)
"extern inline void f(void); in a .c file is meant to be paired with the use of static inline void f(void) {} in header files". Shouldn't you remove the static?Insulation
I've just read the recent C standard draft which states "If all of the file scope declarations for a function in a translation unit include the inline function specifier without extern, then the definition in that translation unit is an inline definition. An inline definition does not provide an external definition for the function, and does not forbid an external definition in another translation unit." Does this mean that the extern declaration is unnecessary if I meet the requirements of an inline definition?Insulation
@xiver77 you're right about removing the static; I re-read the standard just now and realized one of the other canonical answers had misread it. "It is unspecified whether a call to the function uses the inline definition or the external definition. " only applies to calls to inline functions with external linkage, so calls to a static inline cannot call the externally-defined version. (This actually seems to me to provide a barrier to the compiler from doing whole-program optimization)Immoderate
I'm not sure what you mean in your latest question though. In a TU, inline void f(void) {} without extern void f(void); means that it is both an inline definition and a function with external linkage, so calls to the function might be linked to the external definition instead of this TU's inline definition.Immoderate
I now understand. So without the extern declaration, I may not get linker errors if the compiler uses the inline definition, but I do get linker errors if the compiler chooses the external definition. I just have tested with gcc with and without optimizations. Without optimization I get a linker error for not having an external definition.Insulation
@xiver77 yes that sounds right , as in the example in this questionImmoderate
"They ['static inline' and 'static'] are identical in behavior" is confusing since although they are have the scoping/visibility (might call that syntactic behavior) and that the resulting runtime behavior should be logically the same, that misses the point of inline. The resulting code is different (if the compiler does actually inline the code). Runtime behavior will be different since it will run faster which is the whole point.Smasher
I don't, but some consider including a .c file to be 'correct'.Smasher
D
8

From 6.7.4 Function specifiers in C11 specs

6 A function declared with an inline function specifier is an inline function. Making a function an inline function suggests that calls to the function be as fast as possible.138)The extent to which such suggestions are effective is implementation-defined.139)

138) By using, for example, an alternative to the usual function call mechanism, such as inline substitution. Inline substitution is not textual substitution, nor does it create a new function. Therefore, for example, the expansion of a macro used within the body of the function uses the definition it had at the point the function body appears, and not where the function is called; and identifiers refer to the declarations in scope where the body occurs. Likewise, the function has a single address, regardless of the number of inline definitions that occur in addition to the external definition.

139) For example, an implementation might never perform inline substitution, or might only perform inline substitutions to calls in the scope of an inline declaration.

It suggests compiler that this function is widely used and requests to prefer speed in invocation of this function. But with modern intelligent compiler this may be more or less irrelevant as compilers can decide whether a function should be inlined and may ignore the inline request from users, because modern compilers can very effectively decide about how to invoke the functions.

static inline void f(void) {} has no practical difference with static void f(void) {}.

So yes with modern compilers most of the time none. With any compilers there are no practical / observable output differences.

inline void f(void) {} in C doesn't work as the C++ way. How does it work in C?

A function that is inline anywhere must be inline everywhere in C++ and linker does not complain multiple definition error (definition must be same).

What actually does extern inline void f(void); do?

This will provide external linkage to f. Because the f may be present in other compilation unit, a compiler may choose different call mechanism to speed up the calls or may ignore the inline completely.

Deformed answered 29/6, 2015 at 5:20 Comment(0)
C
2

A function where all the declarations (including the definition) mention inline and never extern.
There must be a definition in the same translation unit. The standard refers to this as an inline definition.
No stand-alone object code is emitted, so this definition can't be called from another translation unit.

In this example, all the declarations and definitions use inline but not extern:

// a declaration mentioning inline     
inline int max(int a, int b);

// a definition mentioning inline  
inline int max(int a, int b) {  
  return a > b ? a : b;  
}

Here is a reference which can give you more clarity on the inline functions in C & also on the usage of inline & extern.

Crashaw answered 29/6, 2015 at 5:16 Comment(0)
C
2

If you understand where they come from then you'll understand why they are there.

Both "inline" and "const" are C++ innovations that were eventually retrofit into C. One of the design goals implicit in these innovations, as well as later innovations, like template's and even lambda's, was to carve out the most common use-cases for the pre-processor (particularly, of "#define"), so as to minimize the use of and need for the pre-processor phase.

The occurrence of a pre-processor phase in a language severely limits the ability to provide transparency in the analysis of and translation from a language. This turned what ought to have been easy translation shell scripts into more complicated programs, such as "f2c" (Fortran to C) and the original C++ compiler "cfront" (C++ to C); and to a lesser degree, the "indent" utility. If you've ever had to deal with the translation output of convertors like these (and we have) or with actually making your own translators, then you'll know how much of an issue this is.

The "indent" utility, by the way, balks on the whole issue and just wings it, compromising by just treating macros calls as ordinary variables or function calls, and passing over "#include"'s. The issue will also arise with other tools that may want to do source-to-source conversion/translation, like automated re-engineering, re-coding and re-factoring tools; that is, things that more intelligently automate what you, the programmer, do.

So, the ideal is to reduce dependency on the pre-processor phase to a bare minimum. This is a goal that is good in its own right, independently of how the issue may have been encountered in the past.

Over time, as more and more of the use-cases became known and even standardized in their usage, they were encapsulated formally as language innovations.

One common use-case of "#define" to create manifest constants. To a large extent, this can now be handled be the "const" keyword and (in C++) "constexpr".

Another common use-case of "#define" is to create functions with macros. Much of this is now encapsulated by the "inline" function, and that's what it's meant to replace. The "lambda" construct takes this a step further, in C++.

Both "const" and "inline" were present in C++ from the time of its first external release - release E in February 1985. (We're the ones who transcribed and restored it. Before 2016, it only existed as a badly-clipped printout of several hundred pages.)

Other innovations were added later, like "template" in version 3.0 of cfront (having been accepted in the ANSI X3J16 meeting in 1990) and the lambda construct and "constexpr" much more recently.

Crossbill answered 22/8, 2022 at 0:10 Comment(1)
Nope. I still don't understand inline :) This info is interesting but does not help me understand or use the inline feature.Smasher
M
-2

As the word "Inline" say "In" "Line", adding this keyword to a function affects the program in runtime, when a program is compiled, the code written inside a function is pasted under the function call, as function calls are more costly than inline code, so this optimizes the code.

So, static inline void f(void) {} and static void f(void) {}, here the inline keyword does make a difference in runtime. But when the function has too many lines of code then it won't affect runtime.

If you add static before a function, the function's lifetime is the lifetime of the whole program. And that function use is restricted to that file only.

To know about extern you can refer to - Effects of the extern keyword on C functions

Mahala answered 29/6, 2015 at 5:31 Comment(1)
When the inline keyword was first added to compilers (well before the first C standard), that was its intention. But its meaning and use has changed a lot since then.Immoderate

© 2022 - 2024 — McMap. All rights reserved.