33
#include <stdio.h>
int a[100];
int main(){
    printf("%d",a[5]);
    return 0;
}

Does the above code always print '0' or is it compiler specific? I'm using gcc compiler and I got the output as '0'.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alex
  • 1,178
  • 3
  • 9
  • 24

6 Answers6

55

Yes, all members of a are guaranteed to be initialised to 0.

From section 3.5.7 of the C89 standard

If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.

simonc
  • 41,632
  • 12
  • 85
  • 103
  • 1
    I believe this is from the "X3" ANSI C draft? The same text still holds in the C standard, but why cite such an old, obsolete source? – Lundin Apr 15 '13 at 13:24
  • 1
    @Lundin It was the only source I had to hand and I thought it still made for a better answer than "Yes, the (unreferenced) standard says so". I've +1'd your reference to a more modern standard. – simonc Apr 15 '13 at 13:25
  • 2
    Aha, well the [C11 draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) is easy enough to find on the net. The differences between the draft and the final C11 standard are cosmetic, apart from a [minor corrigendum](http://stackoverflow.com/questions/13914050/what-is-c11-cor-12012/13914051#13914051). – Lundin Apr 15 '13 at 13:28
  • 3
    A change between the 1989 C standard and the current standard is that an uninitialized object with automatic storage duration does **not** have indeterminate value. Generally, the behavior resulting from use of such an object is not defined by the standard. – Eric Postpischil Apr 15 '13 at 13:43
  • @EricPostpischil Thanks. I've removed that part of the quote from my answer. It wasn't required to answer this question and I didn't mean to note any behaviour that didn't also hold for C11. – simonc Apr 15 '13 at 13:45
  • 9
    The best reference would be the *oldest one available*, because it would show the history of how long the rule has been in place. If you give a reference to the C18 standard, I might (incorrectly) assume that this is a new part of the standard. Knowing that C code written in 1975 will have its global uninitialized variables set to zero is *important*. I'd appreciate a reference to the *oldest standard* which establishes this rule. – Christopher Schultz Jan 18 '19 at 17:00
26

"Global variables" are defined at file scope, outside any function. All variables that are defined at file scope and all variables that are declared with the keyword static have something called static storage duration. This means that they will be allocated in a separate part of the memory and exist throughout the whole lifetime of the program.

It also means that they are guaranteed to be initialized to zero on any C compiler.

From the current C standard C11 6.7.9/10:

"... 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;"

Practically, this means that if you initialize your global variable to a given value, it will have that value and it will be allocated in a memory segment usually referred to as .data. If you don't give it a value, it will be allocated in another segment called .bss. Globals will never be allocated on the stack.

Lundin
  • 195,001
  • 40
  • 254
  • 396
7

Yes. Any global variable is initialized to the default value of that type. 0 is the default value and is automatically casted to any type. If it is a pointer, 0 becomes NULL

Global variables get there space in the data segment which is zeroed out.

It is not compiler specific but defined in the C standard.

So it will always print 0.

Sam
  • 1,842
  • 3
  • 19
  • 33
  • XORing with itself is an implementation detail; it doesn't matter *how* the stuff is zeroed. – glglgl Apr 15 '13 at 13:12
  • 3
    Further, I doubt very much that any modern implementation uses XOR to empty memory, as it would incur extra read operations, which is completely unnecessary and adds to the startup time of the application. XOR with itself works well for clearing registers on certain processors that don't have a special instruction for "set register to zero". It is not good for setting memory regions to zero. – Mats Petersson Apr 15 '13 at 13:14
3

File scope objects declared without explicit initializers are initialized by 0 by default (and to NULL for pointers).

Non-static objects at block scope declared without explicit initializers are left uninitialized.

ouah
  • 142,963
  • 15
  • 272
  • 331
1

Are the globle variable always initalized to zero in C?

Yes and It's defined in the C standard.

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0

It is not compiler specific. The code will always print 0.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345