Should C declarations match definition including keywords and qualifiers such as “static”, “inline”, etc

前端 未结 4 652
心在旅途
心在旅途 2020-12-21 02:21

Consider this example of a function declaration and definition (in the same translation unit):

inline static int foo(int x);

...

int foo(int x)
{
  return          


        
4条回答
  •  伪装坚强ぢ
    2020-12-21 02:28

    I want to provide detailed information about inline,

    you can separate the declaration and definition fine, but that definition must be available in every translation unit that uses the function, i.e in your case inline static int foo(int x);

    Inline functions are included in the ISO C99 standard, but there are currently substantial differences between what GCC implements and what the ISO C99 standard requires.

    To declare a function inline, use the inline keyword in its declaration, like this:

    static inline int
    inc (int *a)
    {
      return (*a)++;
    }
    

    An Inline Function is As Fast As a Macro

    Note that certain usages in a function definition can make it unsuitable for inline substitution.

    Note that in C, unlike C++, the inline keyword does not affect the linkage of the function.

提交回复
热议问题