I do not understand the result of this program in C

Here is the code:

#include <stdio.h>

int main (void)
{
    int value[10];
    int index;

    value[0] = 197;
    value[2] = -100;
    value[5] = 350;
    value[3] = value[0] + value[5];
    value[9] = value[5] / 10;
    --value[2];

    for(index = 0; index < 10; ++index)
        printf("value[%i] = %i\n", index, value[index]);
    return 0;
}

Here's the compilation output:

value[0] = 197
value[1] = 0
value[2] = -101
value[3] = 547
value[4] = 0
value[5] = 350
value[6] = 0
value[7] = 0
value[8] = 1784505816
value[9] = 35

I do not understand why the value [8] returns 1784505816? Does the value [8] = the value [6] = the value [7] = 0? By the way, I am compiling code through gcc under Mac OS X Lion.

+5
source share
2 answers

value[8] never initialized, so its contents are undefined and can be anything.

The same applies to the value[1], value[4], value[6], and value[7]. But they just turned out to be zero.

+20
source

, , , . , undefined (, int) .

, , . .

int value[10] = {0};
+6

All Articles