0

I know that there are multiple ways to accomplish this, i.e static could be used on both the declaration and the definition OR it could only be used on the declaration.

Is this the common way of doing so?

// `static` on both the declaration and definition.
static void f1();
static void f1(){}

// `extern` on only the declaration (shared header file).
// then define in a single source file.
extern void f2();
void f2(){}

// `inline` usually doesn't need a declaration, just define it in a shared header file.
inline int f3(){}

int main(){
 return 0;
}
Dan
  • 2,694
  • 1
  • 6
  • 19
  • 1
    Note `extern` is the default for function declarations not marked `static`, so it's optional on `f2`. – aschepler May 21 '21 at 13:53
  • Assume `f2` is a variable instead of a function, then this style would apply right? – Dan May 21 '21 at 13:54
  • 1
    What is the actual question? – SergeyA May 21 '21 at 14:10
  • This is the actual question. I.e do I only put `static` in header declarations, or on both declaration and definition, what is the common way of doing this? – Dan May 21 '21 at 14:16
  • This isn't that difficult of a question, seems pretty clear to me. – Dan May 21 '21 at 14:21
  • 1
    Per C 2018 6.2.2 4, if you first declare an identifier for a function with `static`, then all later declarations will treat it as `static` too, even if it is omitted (as long as the first declaration remains visible). So you can omit it. However, I would regard it as bad form, as it may confuse readers. Putting `static` on each declaration (including definitions) would be clearer. I would answer your question, but including `inline` it in complicates things. You might want to edit this question and ask separately about `inline`. – Eric Postpischil May 21 '21 at 14:38
  • @EricPostpischil thanks, it would be helpful to have them at the same place tho, would appreciate it if you could add the details for inline as well. – Dan May 21 '21 at 14:41
  • 1
    @EricPostpischil I think the complication you are talking about is explained here: https://stackoverflow.com/a/25000931 – Dan May 21 '21 at 15:03

0 Answers0