0

The book "Programming in C" by Stephan Kochan (3rd Edition 2004), states:

One final thing about global variables. They do have default initial values: zero. So, in the global declaration

int gData[100];
all 100 elements of the gData array are set to zero when the program begins execution. So remember that while global variables have default initial values of zero, local variables have no default initial value and so must be explicitly initialized by the program.

On the other hand, discussions on the Internet say global variables would be indetermined or may be luckily set to zero but not necessarily.

Which is true?

Related question: local variable initialized to zero in C

Community
  • 1
  • 1
Wafeeq
  • 869
  • 1
  • 16
  • 34
  • 1
    Whoever says that global or static variables in C or C++ would be indetermined or "luckily" set to zero but not necessarily doesn't have a clue what they are talking about. Someone who puts it into writing left evidence of stupidity. – gnasher729 Jul 06 '16 at 16:33
  • Your book is correct. – FatalError Jul 06 '16 at 16:33
  • 5
    In C, otherwise uninitialized global variables are initialized to "zero" (whatever "zero" means for the specific type). Local non-static variables are not initialized, unless explicitly initialized in the code. – Some programmer dude Jul 06 '16 at 16:33
  • @gnasher729 If speaking strictly of the `C` as per standard - you are right. But people might be referring to the fact that it's often up to the startup code to initialize the BSS. – Eugene Sh. Jul 06 '16 at 16:36
  • How it happens is a matter of implementation. The fact is, when you access them, they will be 0. – FatalError Jul 06 '16 at 16:41
  • so it is useless to initialize global values to their Default? – Wafeeq Jul 06 '16 at 16:41
  • Being explicit isn't a bad thing, either. It's just not required. – FatalError Jul 06 '16 at 16:42
  • What's "Default"? If it is different from `0`, it's not useless. – Eugene Sh. Jul 06 '16 at 16:42
  • for example if I declare an `int`, it would Zero if I dont initilize to Zero. – Wafeeq Jul 06 '16 at 16:43
  • @FatalError Actually it might be sometimes harmful. I remember having a bug in a startup code that was discovered thanks to the main code assuming globals zero, which apparently weren't. – Eugene Sh. Jul 06 '16 at 16:44
  • Are you asking *why* the first kind is set to zero while the second kind is not, or are you asking "which [case] is true"? Or both? – Armen Michaeli Jul 06 '16 at 16:45
  • The reason for the difference has to do with *where* the data is stored. Global variables are in the so-called "BSS" section, which is allocated by the program-loader separately, and set to all-zeros by the loader. Local variables exist on the so-called "stack," which is constantly being re-used. Programming languages customarily do not "scrub the stack" (although some security-enhancement options *do)*. – Mike Robinson Jul 06 '16 at 17:17
  • 1
    As a matter of routine, I suggest: *"Never assume. Never assume ..."* If you want a particular variable to be zero, *set it* to zero. The nano-seconds that you'll spend doing this will be repaid by hours of time *not* spent debugging extremely-difficult(!) problems! (Non-existent statistics show that "garbage in memory" is the number-one cause of hair loss among computer programmers ...) **;-)** – Mike Robinson Jul 06 '16 at 17:20
  • "... is useless to initialize global values to their Default?" --> Functionally it has no value, but from a coding style `int foo = 0;` it conveys explicit intent on the coders part that `foo` should be 0. Perhaps in a later code evolution it be will 42. – chux - Reinstate Monica Jul 06 '16 at 17:28
  • @MikeRobinson +1 for `hair loss` precaution. – Wafeeq Jul 06 '16 at 20:57
  • **:-D** "Fortunately, quite-unlike my older brother, I somehow still have some ..." – Mike Robinson Jul 06 '16 at 23:35

0 Answers0