compile-fan
compile-fan

Reputation: 17665

Are uninitialized globals always default to 0 in c?

I know that uninitialized globals are restored in the BSS segment and the OS should initialize it to zeros.

But it's should not must,and I've never seen any standard saying that uninitialized globals must be default to zeros, so is it safe to take this as granted?

Upvotes: 2

Views: 182

Answers (3)

Mac
Mac

Reputation: 14791

IIRC there's certainly no requirement that uninitialised globals be set to zero, and I'm sure I've heard of cases where that's not the case. As always, play it safe and always initialise your variables yourself if you're afraid of it being an issue.

I personally try to never take anything for granted. Not only does making it explicit neatly circumvent any such troubles, but it also makes it clear for anyone else reading your code what you are expecting to be the case.

EDIT: I've since been corrected that the standards do require globals to be initialised to zero. Just to clarify my poor wording above, I don't mean that absolutely nothing can ever be taken for granted (that's absurd), but rather that if there is a simple and concise way of not taking something for granted, do it.

The reason I advocate this approach is because although most programmers can rely on compilers with standards-compliant behaviour, there are plenty of us that work in environments where standards compliance is not always possible for whatever reason (the hardware limitations of microcontrollers being a good example, or see Steve's example in the comments). I'd also argue that there's not in existence any compiler that is fully standards compliant (other than in those cases where a compiler defines the standard).

When I see int myGlobal=0; in a file, I know for sure that myGlobal has a value of zero. If it is just declared as int myGlobal;, the standard says that is also should have a value of zero. That does not guarantee that it will, and I believe that typing the extra two characters is no big cost, increases readability of the program, and increases portability if you find you ever do need to compile the code on a platform that doesn't pre-initialise globals. That is my point - why not, and you might just cover yourself even if the standard says you should be ok.

Upvotes: 0

janneb
janneb

Reputation: 37248

The C standard says that variables with static storage duration (which includes all global variables) without explicit initialization are initialized to zero.

Other languages, such as Fortran, differ.

Upvotes: 3

odrm
odrm

Reputation: 5269

Here's the autoritative answer from the C99 Standard Document clause 6.7.8 (paragraph 10):

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static 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;
  • if it is a union, the first named member is initialized (recursively) according to these rules.

Upvotes: 5

Related Questions