F
fox
How do I (portably) make a forward reference to a static (I mean
file-scope) variable?
I've been using "extern" for years, for example:
extern int x;
int foo(void)
{
return x++;
}
static int x = 5;
.... and gcc 3.4.4 and MSVC 2005 do not even warn that anything's wrong
here.
But gcc 4 gives me an error:
static declaration of 'x' follows non-static declaration
It seems that replacing "extern" with "static" makes it happy.
I always thought that "static" means a definition (and not merely a
declaration) for variables
and if you want to just declare a variable then you use "extern". Now I
cannot understand what "static int x;" actually is: just a declaration?
or declaration and definition?
Please note that I can remove the second "static int x" line and it
still compiles...
Can you explain me what C standards say about this issue and why some
compilers don't issue a warning while gcc 4 gives an error? Is it
because some compilers default to C89 and the other default to C9X ?
Thanks,
Piotr
file-scope) variable?
I've been using "extern" for years, for example:
extern int x;
int foo(void)
{
return x++;
}
static int x = 5;
.... and gcc 3.4.4 and MSVC 2005 do not even warn that anything's wrong
here.
But gcc 4 gives me an error:
static declaration of 'x' follows non-static declaration
It seems that replacing "extern" with "static" makes it happy.
I always thought that "static" means a definition (and not merely a
declaration) for variables
and if you want to just declare a variable then you use "extern". Now I
cannot understand what "static int x;" actually is: just a declaration?
or declaration and definition?
Please note that I can remove the second "static int x" line and it
still compiles...
Can you explain me what C standards say about this issue and why some
compilers don't issue a warning while gcc 4 gives an error? Is it
because some compilers default to C89 and the other default to C9X ?
Thanks,
Piotr