174

IMO both make the function to have a scope of the translation unit only.

What's the difference between "static" and "static inline" function?

Why should inline be put in a header file, not in .c file?

0

7 Answers 7

146

By default, an inline definition is only valid in the current translation unit.

If the storage class is extern, the identifier has external linkage and the inline definition also provides the external definition.

If the storage class is static, the identifier has internal linkage and the inline definition is invisible in other translation units.

If the storage class is unspecified, the inline definition is only visible in the current translation unit, but the identifier still has external linkage and an external definition must be provided in a different translation unit. The compiler is free to use either the inline or the external definition if the function is called within the current translation unit.

As the compiler is free to inline (and to not inline) any function whose definition is visible in the current translation unit (and, thanks to link-time optimizations, even in different translation units, though the C standard doesn't really account for that), for most practical purposes, there's no difference between static and static inline function definitions.

The inline specifier (like the register storage class) is only a compiler hint, and the compiler is free to completely ignore it. Standards-compliant non-optimizing compilers only have to honor their side-effects, and optimizing compilers will do these optimizations with or without explicit hints.

inline and register are not useless, though, as they instruct the compiler to throw errors when the programmer writes code that would make the optimizations impossible: An external inline definition can't reference identifiers with internal linkage (as these would be unavailable in a different translation unit) or define modifiable local variables with static storage duration (as these wouldn't share state accross translation units), and you can't take addresses of register-qualified variables.

Personally, I use the convention to mark static function definitions within headers also inline, as the main reason for putting function definitions in header files is to make them inlinable.

In general, I only use static inline function and static const object definitions in addition to extern declarations within headers.

I've never written an inline function with a storage class different from static.

8
  • 10
    I read your entire answer and I still don't get the semantic difference between static and static inline. Both of them make the definition invisible to other translation units. So what would be a sensible reason to write static inline instead of static?
    – user541686
    Aug 4, 2020 at 23:20
  • 2
    +1 for the hugely insightful reminder that they are not useless, because they instruct the compiler to treat impossibility to do those optimizations as an error!
    – mtraceur
    Sep 25, 2020 at 20:56
  • 3
    @user541686 The biggest and most important semantic difference is that static inline expresses your intent/approval for it to be inlined, whereas static does not. To be clear about definitions, "semantics" is basically a fancy word for "meaning", and so that is the most essential semantic difference right there. Because source code is first and foremost about describing what you intend for the code to do. And if you proactively intend for inlining to happen, you should say so, and inline is the most "native" way to say so in the language of C.
    – mtraceur
    Sep 25, 2020 at 21:05
  • 1
    @jdk1-0 Intent is ideally communicated to both humans and software. Humans first and foremost, but the compiler and other tools can do a better job at optimizing or detecting problems if you can tell them your intent. So in the intent perspective, the difference between static inline and static INLINE_INTENT is that one of them also reveals your intent to compilers and other software (because we standardized on that as the way to express that specific intent).
    – mtraceur
    May 21, 2021 at 17:25
  • 1
    Clang llvm uses a different threshold for inline specified functions: godbolt.org/z/rar9adfbv I dare say clang trunk is a "modern compiler"
    – Etienne M
    Dec 11, 2021 at 23:14
122

inline instructs the compiler to attempt to embed the function content into the calling code instead of executing an actual call.

For small functions that are called frequently that can make a big performance difference.

However, this is only a "hint", and the compiler may ignore it, and most compilers will try to "inline" even when the keyword is not used, as part of the optimizations, where its possible.

for example:

static int Inc(int i) {return i+1};
.... // some code
int i;
.... // some more code
for (i=0; i<999999; i = Inc(i)) {/*do something here*/};

This tight loop will perform a function call on each iteration, and the function content is actually significantly less than the code the compiler needs to put to perform the call. inline will essentially instruct the compiler to convert the code above into an equivalent of:

 int i;
 ....
 for (i=0; i<999999; i = i+1) { /* do something here */};

Skipping the actual function call and return

Obviously this is an example to show the point, not a real piece of code.

static refers to the scope. In C it means that the function/variable can only be used within the same translation unit.

13
  • 8
    It's also important to note that code declared as inline belongs in the header, where as normal (non-template) source code cannot go in headers without causing multiple redefinition errors. So even when declaring something inline, even if the compiler chooses not to inline it, there is still a standard-mandated multiple-redefinition avoiding behavior that kicks in.
    – VoidStar
    Oct 14, 2011 at 4:08
  • 14
    @VoidStar Actually static (with or without inline) can be in header perfectly well, see no reason why not. Templates is for C++, this question is about C.
    – littleadv
    Oct 14, 2011 at 5:46
  • 2
    @littleadv: the main reason for putting function definitions in header files is to make them inlinable, so marking them explicitly inline is good style, imo
    – Christoph
    Oct 14, 2011 at 12:55
  • 2
    Have you actually tried compiling your example into assembly? At least when I tried it with GCC 4.9, with no -O flag, or if -O0 is specified, the function call is still there. If you compile it with -O1 or above, now it is actually inlined, but even not specifying inline still inlines it, so there is no difference between static and static inline.
    – syockit
    Mar 9, 2017 at 0:26
  • 22
    Actually inline doesn't instruct the compiler to do any attempts at inlining. It merely lets the programmer to include function body in multiple translation units without ODR violation. A side effect of this is that it's makes it possible for the compiler, when it would inline the function, to actually do this.
    – Ruslan
    Jan 2, 2018 at 19:03
32

From my experience with GCC I know that static and static inline differs in a way how compiler issue warnings about unused functions. More precisely when you declare static function and it isn't used in current translation unit then compiler produce warning about unused function, but you can inhibit that warning with changing it to static inline.

Thus I tend to think that static should be used in translation units and benefit from extra check compiler does to find unused functions. And static inline should be used in header files to provide functions that can be in-lined (due to absence of external linkage) without issuing warnings.

Unfortunately I cannot find any evidence for that logic. Even from GCC documentation I wasn't able to conclude that inline inhibits unused function warnings. I'd appreciate if someone will share links to description of that.

2
  • 1
    Mmm, still got warning: unused function 'function' [clang-diagnostic-unused-function] for a static inline function when building withclang-tidy (v8.0.1), which is used in another translation unit. But definitely, this is one of the best explanations and reason for combining static & inline!
    – jaques-sam
    Apr 29, 2020 at 12:02
  • @DrumM, but ony was referring to gcc. But you're right, this is probably the best explanation so far. May 20, 2021 at 14:43
8

One difference that's not at the language level but the popular implementation level: certain versions of gcc will remove unreferenced static inline functions from output by default, but will keep plain static functions even if unreferenced. I'm not sure which versions this applies to, but from a practical standpoint it means it may be a good idea to always use inline for static functions in headers.

4
  • What about using inline in definition? Do you also imply not using it for extern functions?
    – new_perl
    Oct 14, 2011 at 4:02
  • Is this still true with recent version of GCC? Your answer would be a lot more interesting if you an example and listed the which version of GCC do it.
    – Z boson
    Dec 21, 2015 at 12:41
  • @Zboson: I don't have that information readily available and don't have time to setup and test lots of gcc versions at the moment, but I agree it would be useful information to have. You could probably find when gcc first started optimizing out unused static functions/objects by looking at the history of attribute((used)) and its use to allow asm to reference otherwise-unreferenced static functions and data. Dec 21, 2015 at 18:07
  • major versions of gcc from 4.1 all the way to 11.1 will remove unused static and unused static inline functions over at godbolt.org. And they all warn about unused for static functions, but none warn for unused static inline. Also, unspecified storage class functions will remain if unused, without warning (even with optimizations). But I don't know how to test with headers on that site. (And I would get linker errors on anything above gcc 4.9.x when trying to compile with just inline.) May 20, 2021 at 15:19
8

In C, static means the function or variable you define can be only used in this file(i.e. the compile unit)

So, static inline means the inline function which can be used in this file only.

EDIT:

The compile unit should be The Translation Unit

4
  • 2
    Or in fancy words: it has internal linkage.
    – K-ballo
    Oct 14, 2011 at 3:34
  • @AlokSave: Is there a difference between compilation unit and translation unit? If so, which is more appropriate in the context of the C++ language?
    – legends2k
    Oct 11, 2013 at 0:57
  • 2
    I believe the compile unit is something I wrote in error, there is no such thing, the actual terminology is translation unit
    – shengy
    Oct 11, 2013 at 0:59
  • Your answer is not complete because it's mostly used in header files, accross translation units.
    – jaques-sam
    Apr 29, 2020 at 12:05
2

In C++, one important effect of inline (that is not mentioned in the other answers yet, I think) is that it prevents linker errors when multiple definitions of the function are found.

Consider a function that is defined in a header file to allow it to be inlined into the source files that include the header. If the compiler decides to not inline (all calls to) this function, the function definition will be included into every object file that references it (i.e. does not inline all calls).

This might cause multiple definitions of the functions to read the linker (though not always, since it depends on the inlining decisions made by the compiler). Without the inline keyword, this produces a linker error, but the inline keyword tells the linker to just pick one definition and discard the rest (which are expected to be equal, but this is not checked).

The static keyword, on the other hand, ensures that if a function is included in the object file, it will be private to that object file. If multiple object files contain the same function, they will coexist and all calls to the function will use their "own" version. This means that more memory is taken up. In practice, I believe this means that using static for functions defined in header files is not a good idea, better to just use inline.

In practice, this also means that static functions cannot produce linker errors, so the effect of inline above is not really useful for static functions. However, as suggested by ony in another answer, adding inline might be helpful to prevent warnings for unused functions.

Note that the above is true for C++. In C, inline works a bit different, and you have to explicitly put an extern declaration in a single source file to have the inline function emitted into that object file so it is available for any non-inlined uses. In other words, inline means that a function is not emitted into any source file, even when not all calls are inlined, unless it is also specified as extern, and then it is emitted (even if all local calls are inlined). I'm not sure how that interacts with static, though.

0

An inline definition is not externally linked.

// average.h
#ifndef AVERAGE_H
#define AVERAGE_H
inline double average(double a, double b);
#endif

Attempting to call an inline function with the definition above from another module after it has been preprocessed or linked to a c file will result in an error.

There are two ways to solve this problem:

  1. make it a static inline function defintion. Example:
// average.h
#ifndef AVERAGE_H
#define AVERAGE_H
static inline double average(double a, double b);
#endif
  1. include the defintion from the c file and make it external. Example:
#include "average.h"
extern double average(double a ,double b){ 
  return (a + b) / 2;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.