3

By running this code

char array[6];
int i;
for ( i = 0; i < 6; ++i )
    printf("%i ", array[i]);

Possible output:

64 0 -64 77 67 0

I get always the last element 0, although I was expecting random value. It is compiler dependent? I'm using gcc.

Tom
  • 1,027
  • 2
  • 16
  • 35
  • 5
    You don't get _random_ numbers, but you get _indeterminate_ numbers. – Jabberwocky Oct 19 '16 at 13:59
  • 2
    The simple answer - No – ryyker Oct 19 '16 at 14:00
  • 1
    Curious - _always the last element 0_. How many times is _always_ ? – ryyker Oct 19 '16 at 14:07
  • @ryyker I get **always** last element 0, no matter if I run this code 10 or 10000 times, no matter if the array has size 5 or 5000 – Tom Oct 19 '16 at 14:10
  • 3
    Due to the automatic storage of your array, probably, the code before the function where the code is placed, leave that 0 into the stack. – LPs Oct 19 '16 at 14:12
  • 2
    @Tom don't waste anymore time on this, or step through the assymbly code with a debugger if you want to know what exactly happens in your case. – Jabberwocky Oct 19 '16 at 14:21
  • I would be curious to know what would happen if you completely closed your environment between compiles. I have observed behaviors where something that should be randomly changing does not change between compiles. But closing Code::Blocks, which also uses GCC compiler, (i.e., dumping it from memory) then re-opening, I see that the value changes from what it was during the compilers previous life. – ryyker Oct 19 '16 at 14:27
  • @ryyker I have completely closed my environtment and then tried building, rebuilding, closing again, rebuilding but still the same result :) I'am also using Code::Blocks. – Tom Oct 19 '16 at 15:02
  • @Tom - I am not sure if the question's _duplicate_ status prevents it, but selecting one of the answers as _Accepted_ is always appreciated. – ryyker Oct 19 '16 at 16:44
  • @rykker Yes I know and I will do it, but I have not decided yet which one ;) – Tom Oct 19 '16 at 18:45

3 Answers3

4

No. There's no such thing guaranteed by the C standard for local variables.

The values of the uninitialized array has indeterminate values. So, you can't access them and since you do, your code has undefined behaviour.

But the variables with static storage duration such as global variables, static qualified variables etc are initialized with zero.

P.P
  • 117,907
  • 20
  • 175
  • 238
3

The contents of a variable (and by extension, the elements of an array) that do not have static storage duration (globals, static locals) are undefined.

The fact that the last element in the array happens to be 0 essentially is random.

dbush
  • 205,898
  • 23
  • 218
  • 273
3

Last element is zero in string constants like "Test" or char array[] = "Test";. In your example last value is zero by chance.

Try this:

void f1() // prepare non-zero stack
{
  char array[40];

  memset( array, 32, sizeof array ); 
}

void f2() // your array
{
  char array[6];
  int i;
  for ( i = 0; i < 6; ++i )
    printf("%i ", array[i]);
}

int main()
{
  f1();
  f2();
  return 0;
}
i486
  • 6,491
  • 4
  • 24
  • 41
  • OP includes statement: _I get always the last element 0_. If this is an accurate observation (as opposed to an exaggeration) then _by chance_ may not be accurate. – ryyker Oct 19 '16 at 14:11
  • @ryyker It is by chance. I can show example how to get value other than 0. (Has to fill the stack before calling such function.) And the question is "Does C always initialize...", not "is this in my case". – i486 Oct 19 '16 at 14:14
  • @i486 I would be grateful to see this example, can you show it please? – Tom Oct 19 '16 at 14:22
  • 1
    @Tom Try example in the answer above. – i486 Oct 19 '16 at 14:25
  • @i486 You have right, now I don't get 0 anymore, Thanks! – Tom Oct 19 '16 at 14:31