2

I am learning C language, during which, I confront with global variables and extern keyword. I read about them on this site (Global variable in C are static or not) and found that

Global variables are extern by default

But I know that global variable's default value is 0.
So, I am confused: how can it be possible that variables with extern keyword are only declared and not defined.

Community
  • 1
  • 1
Srshti
  • 49
  • 5

1 Answers1

2

So, I am confused how can it be possible, as variables with extern keyword are only declared not defined.

  • Global variables are extern by default - that means they can be used by other translation unit (can simply be other source files).
  • That said global variables are not automatically usable from other source files unless there are corresponding extern declaration on those source files.
  • True that the extern keyword does not have any initialisation because it is declaration only. So the auto value (0) of global variables (from original source files) is obtained at the point their own definition.
  • Note that there can only be one definition (in original source file), but as many extern declarations (on other source files).
Community
  • 1
  • 1
artm
  • 17,291
  • 6
  • 38
  • 54