I am learning about Arrays in the book 'Programming in C' by Stephen G. Kochan. I have reached a point where I am getting really confused by the output, despite long time trying to find syntax errors.
Here is my code:
#include <stdio.h>
int main (void)
{
int values[10];
int index;
values[0] = 197;
values[2] = -100;
values[5] = 350;
values[3] = values[0] + values[5];
values[9] =
values[5] / 10;
--values[2];
for ( index = 0; index < 10; ++index )
printf("values[%i] = %i\n", index, values[index]);
return 0;
}
How the output is supposed to look according to the book:
values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35
How my output looks like:
values[0] = 197
values[1] = -2
values[2] = -101
values[3] = 547
values[4] = 4200832
values[5] = 350
values[6] = 4200926
values[7] = 4200832
values[8] = 7680288
values[9] = 35
It is like all the zeros (uninitialized values) have been replaced with these big numbers. What is going on, what how can I change it?
UPDATE
Peter Griffiths mentions this, which is a quote from the book "Programming in C 3rd edition" by Stephen G. Kochan:
Because you never assigned values to five of the elements in the array - elements 1, 4, and 6 through 8 - the values that are displayed for them are meaningless. Even though the program's output shows these values as zero, the value of any uninitialized variable or array element is not defined. For this reason, no assumption should ever be made as to the value of an uninitialized variable or array element.
This clearly state that there is nothing wrong with the book. SO IT IS NOT A BAD BOOK! ONLY GOOD EXPERIENCES SO FAR!
I am using Windows 7 Home Premium 64-bit and I am using the gcc (GCC) 4.7.2 compiler. The reason for the different output is probably different OS and Compiler.