How many instance of the variable will be produced if I declared a static
global variable in one header file, and two .c / .cpp files included the
header file? Example as the following:

<abc.h>
static int abc = 1;
...

<abc1.c>
#include "abc.h"
void ChkAbc1()
{
int* Addr = &abc;
}
...

<abc2.c>
#include "abc.h"
void ChkAbc2()
{
int* Addr = &abc;
}
...

One more question, if a static global variable declared in a .c / .cpp file,
can I extern this variable on the header file? Example as the following:

<abc.h>
extern int abc;
...

<abc.c>
#include "abc.h"
static int abc = 1;
...

<main.c>
#include "abc.h"
void main()
{
int* Addr = &abc;
}
...


Thanks in advance,

Best Regards,
Elliott

Re: static global variable by Carl

Carl
Fri Apr 23 00:29:11 CDT 2004

Elliott wrote:
> How many instance of the variable will be produced if I declared a
> static global variable in one header file, and two .c / .cpp files
> included the header file? Example as the following:

Two - one in each .obj.

> One more question, if a static global variable declared in a .c /
> .cpp file, can I extern this variable on the header file? Example as
> the following:

Leave the static off

>
> <abc.h>
> extern int abc;
> ...
>
> <abc.c>
> #include "abc.h"
> static int abc = 1;

replace 'static' with 'extern' in the above, or just leave it off - abc is
already extern.

-cd



Re: static global variable by Elliott

Elliott
Fri Apr 23 00:54:09 CDT 2004

I got it. Thanks,

"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:urVPrPPKEHA.3628@TK2MSFTNGP12.phx.gbl...
> Elliott wrote:
> > How many instance of the variable will be produced if I declared a
> > static global variable in one header file, and two .c / .cpp files
> > included the header file? Example as the following:
>
> Two - one in each .obj.
>
> > One more question, if a static global variable declared in a .c /
> > .cpp file, can I extern this variable on the header file? Example as
> > the following:
>
> Leave the static off
>
> >
> > <abc.h>
> > extern int abc;
> > ...
> >
> > <abc.c>
> > #include "abc.h"
> > static int abc = 1;
>
> replace 'static' with 'extern' in the above, or just leave it off - abc is
> already extern.
>
> -cd
>
>