0

I read about global variables in C. I have two questions about the extern keyword.

  1. Why is initialization required for variables that are declared globally with the extern keyword?
  2. Why is initialization not required for variables that declared globally without the extern keyword?

Please explain the difference.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Are you sure about your definitions? Most often, `extern SomeType some_variable;` is used without an initialization in a header file to declare the variable, and `SomeType some_variable = { … };` is used to define and initialize the variable in one source file. That seems to be the opposite of what you're claiming. See [How do I use `extern` to share variables between source files in C](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c/) for the nitty-gritty details. Stop reading at one of the two earlier stopping points (you'll see). – Jonathan Leffler Sep 03 '16 at 06:57
  • initialization is *not* required for an `extern` variable. an `extern` variable is explicitly *not* defined in this file, but is instead a reference to a variable defined in some other object file that will be linked with this one. – J Earls Sep 03 '16 at 07:15

2 Answers2

2
  1. Why is initialization required for variables that are declared globally with extern?

    // declarations  
    extern int foo1;
    extern int foo2;
    

Initialization is not required. Global variables lacking explicit initialization are initialized as below.

Declaring a variable does not define it. In some unit, the variable, if used, needs to be defined.

  1. Why is initialization not required for variables that declared globally without extern?

    // declaration and definition - no initialization
    int foo3;  // Same functional effect as int foo3 = 0;
    // declaration and definition - with initialization
    int foo4 = 5;
    

Global variables lacking explicit initialization are initialized as below.


C11dr 6.7.9 Initialization

... If an object that has static or thread storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

Why is initialization required for variables that are declared globally with the extern keyword?

When we use extern keyword with uninitialized variable compiler think variable has initialized somewhere else in the program. For example:

#include <stdio.h>
extern int i;    //extern variable
int main()
{
    printf("%d",i);
    return 0;
}

Output:

 undefined reference to `i'

because, memory is not allocated for these variable. Hence in second case compiler is showing error unknown symbol i. That's why allocate the memory for extern variables it is necessary to initialize the variables. Like,

extern int i;
int i = 10;

Why is initialization not required for variables that declared globally without the extern keyword?

When we don't use extern keyword then compiler initialized it with default value. For example:

#include <stdio.h>
int i;    //By default it is extern variable
int main()
{
    printf("%d",i);
    return 0;
}

Output:

0

because, Compiler will automatically initialize with default value to extern variable.

msc
  • 33,420
  • 29
  • 119
  • 214