I have been investigating when it is possible to mix variables declared with extern, static and no storage specifier in global scope. The results have left me quite confused.
This is what I found (each paragraph is a separate compilation unit):
/* ok */
int x;
int x;
/* ok */
int f();
int f();
/* ok */
int x;
extern int x;
/* ok */
int f();
extern int f();
/* error: static declaration follows non-static declaration */
int x;
static int x;
/* ok (no warning) */
int f();
static int f();
/* ok */
extern int x;
int x;
/* ok */
extern int f();
int f();
/* ok */
extern int x;
extern int x;
/* ok */
extern int f();
extern int f();
/* error: static declaration follows non-static declaration */
extern int x;
static int x;
/* error: static declaration follows non-static declaration */
extern int f();
static int f();
/* error: non-static declaration follows static declaration */
static int x;
int x;
/* ok (no warning) */
static int f();
int f();
/* ok */
static int x;
extern int x;
/* ok */
static int f();
extern int f();
/* ok */
static int x;
static int x;
/* ok */
static int f();
static int f();
I get the same exact results with gcc and clang but I cannot find a pattern in what works and what doesn't work.
Is there any logic here?
What does the C standards say about mixing global declarations declared with extern, static and no storage specifier?