Skip to main content
2 of 2
edited tags
Vlad from Moscow
  • 313.1k
  • 27
  • 204
  • 358

Using static keyword in definition vs declaration in C

The following compiles fine, using static only during declaration of function:

#include <stdio.h> static int a(); int a(){ return 5; } int main(){ printf("%d\n", a()); return 0; } 

As a side note, same behaviour as above happens with inline functions, i.e only the declaration could have the keyword.

However the following fails, doing the same but on a variable:

#include <stdio.h> static int a; int a = 5; int main(){ printf("%d\n", a); return 0; } 

Getting thew error: non-static declaration of 'a' follows static declaration.

What is with the difference?

user13744616