-2

I have the following piece of code where I define a couple of external (global) variables after the place in code where I need to use them. In order to do so I use the keyword extern to declare them without reserving storage for them.

int main(int argc,char *argv[])
{

  extern int a;
  extern double b;

  /* ...use the variables somehow... */

{

int a = 10;
static double b = 2.0;

if I do so, the compiler complains that I'm defining the b variable to be static (thus with internal linkage),when before I declared it to be extern. But if I invert the order and define it before using it and declare it inside main ( which is otpional I know...) everithing is fine.

static double b = 2.0;

int main(int argc,char *argv[])
{

  extern int a;
  extern double b;

  /* ...use the variables somehow... */

{

int a = 10;

so what if I'd like to use an external private variable (i.e. with internal linkage) before I define it? is it forbidden and why?

Luca
  • 1,658
  • 4
  • 20
  • 41

1 Answers1

1

The extern keyword tells the compiler that the variable we refer to is located in a different translation unit (another source file basically), while the static keyword means that the variable (in the case of global variables) is local to the current translation unit and cannot be seen in other source files, so it makes no sense to use the two keywords together.

Once you have declared b as global in the same file, it is visible in main and there is no need to declare it again, you just can use it. If on the other hand it is declared in a different translation unit as a global variable, the extern keyword becomes necessary.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
monkeyStix
  • 620
  • 5
  • 10
  • yes, but what if I want to use it before I define (reserve storage for) it ? Does it need to be a shared global?? What if I wanted it to be a private global (with internal linkage) – Luca Jul 14 '16 at 13:19
  • storage for globals is reserved in the data segment of a program, and you cannot use a global before you declare it (with the exception of extern, but as i said this is used for variables from different translation units). – monkeyStix Jul 14 '16 at 13:40
  • You should format your answer and use capitals at the beginning of a sentence. – Jabberwocky Jul 14 '16 at 13:41
  • The `static` keyword only makes file-scope identifiers local to the current translation unit. `static` has a completely different effect for automatic variables, where it changes the storage duration of the object from the scope of its definition with an instance of the object that's initialized for each thread of execution to a single instance of the object that exists for the entire lifetime of the process with initialization at process start. – Andrew Henle Jul 14 '16 at 13:56
  • @Andrew Henle you are correct, edited the post to make it explicit that i am only referring to global variables to avoid confusion, thanks for the input – monkeyStix Jul 14 '16 at 14:03
  • I can use *extern* with a global variable which is defined in the same translation unit,but later in the file. I want to do the same thing with a *static* global. No one has answered my question. – Luca Jul 14 '16 at 16:37