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.
Here's a quote from that book, from the paragraph immediately following the output you quote:
Because you never assigned
valuesto five of the elements in the array - elements 1, 4, and 6 through 8 - thevaluesthat 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.
Moral: you get a lot more out of programming books when you actually read them.
In C the elements of an array are not initialized by default.
Just try :
int values[10] = {0};
In Programming, it is a good practice to assume that every non initialised pointers and non-pointer variables contain garbage.
EDIT :
If this book don't explain you that... I will do like others and suggest you to find a new book....
It seems that this book actually talk about the garbage values you may find with uninitialise variables...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With