Search code examples
cstaticinlinec99function-declaration

static inline vs inline static


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)


Solution

  • 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.