0

As specified in the Standard

If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier extern.

But function specifier part gives inline function semantic as follows:

Any function with internal linkage can be an inline function. For a function with external linkage, the following restrictions apply: If a function is declared with an inline function specifier, then it shall also be defined in the same translation unit. 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.

Case 1.

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

inline void test(void); //does it provide an external definition?

Case 2.

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

extern inline void test(void); //does it provide an external definition?

Case 3.

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

void test(void); //does it provide an external definition?

I have a confusion regarding the three cases. Are there differences between them? I currently think about them as

Case 1 -- does not provide an external definition (inline without extern)

Case 2 -- provides external definition (inline with extern)

Case 3 -- provides external definition (same as with extern)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • See • [When to use `inline` function and when not to use it?](https://stackoverflow.com/questions/1932311/when-to-use-inline-function-and-when-not-to-use-it) • **[Is `inline` without `static` or `extern` ever useful in C99?](https://stackoverflow.com/questions/6312597/is-inline-without-static-or-extern-ever-useful-in-c99)** • [`extern inline`](https://stackoverflow.com/questions/216510/extern-inline) • [What is the use of the `inline` keyword in C?](https://stackoverflow.com/questions/31108159/what-is-the-use-of-the-inline-keyword-in-c) – Jonathan Leffler Oct 18 '19 at 10:24

1 Answers1

2

static and extern cannot go together.

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

inline void test(void); //does it provide an external definition?

should actually be

static inline void test(void){ //internal linkage, can be an inline function
    printf("Test\n");
}

static inline void test(void); //does it provide an external definition?

because the definition and the declaration should match. I am not sure though that static actually needs to be used when also using inline.


Case 2 -- provides external definition (inline with extern)

Case 3 -- provides external definition (same as with extern)

these actually conflict (if I understand correctly) with:

Any function with internal linkage can be an inline function.

extern is exactly about external linkage, as opposed to internal.

Community
  • 1
  • 1
virolino
  • 2,073
  • 5
  • 21
  • _static and extern cannot go together_. How about this: _For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible, 31) if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration._ So `static` cannot go after `extern`, but can go before it. – St.Antario Oct 18 '19 at 06:06
  • That might be true. But why should one actually do that? I mean, IOCCC is full of ways of abusing the C language. `static` and `extern` were created (primarily) for opposite purposes. I would never mix them just because I found a compilable way to do that. – virolino Oct 18 '19 at 06:29