static inline vs inline static

Viewed 1796

I have noticed that both work, what is the correct way to use inline here?

static inline int getAreaIndex()

OR

inline static int getAreaIndex()

Plus, getAreaIndex contains a large loop. sometimes I call it only one and sometimes I call it through a loop, should I inline it? (it's 10 line tall)

5 Answers

What is the correct way to use inline here

Both static inline and inline static are allowed and they mean the same thing. static inline is preferred, because "storage class specifiers" like static are always supposed to come first in a declaration (see C11 §6.11.5).

should I inline this function

To answer this question you will need to benchmark your program both ways and find out which is faster.

From the C standard (6.7 Declarations)

declaration:
    declaration-specifiers init-declarator-listopt ;
    static_assert-declaration

declaration-specifiers:
    storage-class-specifier declaration-specifiersopt
    type-specifier declaration-specifiersopt
    type-qualifier declaration-specifiersopt
    function-specifier declaration-specifiersopt
    alignment-specifier declaration-specifiersopt

It means that you may specify declaration specifiers in any order.

So for example all shown below function declarations declare the same one function.

#include <stdio.h>

static inline int getAreaIndex( void );
inline static int getAreaIndex( void );
int static inline getAreaIndex( void );
static int inline getAreaIndex( void );
inline int static getAreaIndex( void )
{
    return  0;
}


int main(void) 
{
    return 0;
}

As for the inline function specifier then according to the C Standard (6.7.4 Function specifiers)

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.

and there is a footnote

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

Pay attention to that you should specify as the function parameter void. Otherwise the compiler will decide that the number and types of arguments are deduced from a function call.

Function specifiers, such as inline, and storage class specifiers, such as static, may appear in any order as part of a function declaration.

So both examples above mean exactly the same thing.

As for whether you should inline, the details of exactly what inline does implementation defined. So you should look up the documentation of your compiler to see.

They are functionally equivalent at the moment, but static inline is the correct way to write C. This is because of C17 having made other styles obsolete and bad practice:

6.11.5 Storage-class specifier

The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.

static being a "storage class specifier".

should I inline it?

Inline is just a hint for the compiler and the compiler is free to disregard it if it would be too detrimental for performance.

Also, for locally defined functions (in the same .c file/translation unit), the compiler can freely decide to inline a function, even if it was not marked as such.

In most cases, for static function not defined in headers, I believe it is preferred not to specify inline, and let the compiler inline the function as it sees fit, according to the optimization options you are providing it (-O<n>, -Ofast, -Osize).

Related