Reputation: 97
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.
Upvotes: 2
Views: 1145
Reputation: 14510
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...
Upvotes: 7
Reputation: 25908
Here's a quote from that book, from the paragraph immediately following the output you quote:
Because you never assigned
values
to five of the elements in the array - elements 1, 4, and 6 through 8 - thevalues
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.
Moral: you get a lot more out of programming books when you actually read them.
Upvotes: 8
Reputation: 3143
Initialize the array. You haven't initialized the array so it's filled with garbage values. Do it like:
int values[10] = {0};
Good practice is to always initialize the variables. And i suggest you changing your book as well. Try "C -How to Program by Dietel & Dietel"
Upvotes: 0
Reputation: 1927
You don't appear to have any errors in your syntax. Memory is reused by the system very quickly, so quickly that the system doesn't bother resetting all values to 0 prior to handing the memory over to a process to be used.
It is good programming practice to always assume that your variables contain garbage until you have assigned a value to them. This applies to pointers just as much as non-pointer variables. An array variable in C/C++ is really a pointer to a continuous block of memory, and as such the system allocates that memory by an internal process that is primarily tuned for speed. Taking the time to set all values in memory blocks to 0 would reduce the speed at which the system returns memory to you; further, it is assumed that in almost all cases you really won't want a big block of zero values to carry around, instead you will be putting your own data in.
Check the book you are reading to see if there is a note later on about this behavior. If such a note does not exist in the book, you might consider reading alternate sources to make sure that you don't run into other assumptions made by the writer(s) that may be inaccurate.
Upvotes: 2
Reputation: 58271
you are getting garbage values at index 4, 6, 7, 8.., default values of array elements are garbage( if you do not initialize), you should declare your array and initialized you array with 0
value, just to like:
int values[10] = {0};
This will set all value[i]
to zero. (remember incomplete initialization set uninitialized values with 0).
Upvotes: 1
Reputation: 1
Like what everyone else said. The uninitialized values are not guaranteed to be 0. This is true for all variables, not just ints. A good practice is to initialize all variables to some default value just to make sure it doesn't have garbage values that could mess you up later.
Upvotes: 0
Reputation: 241641
C does not zero the elements of an array. That's why those elements are not initialized to zero; you're just seeing garbage values from whatever was already in those memory locations.
If your book says that C does zero the elements of an array, get a new book.
Upvotes: 3
Reputation: 500357
You don't initialize elements 1, 4, 6, 7, and 8. To set the entire array to zeros, initialize it like so:
int values[10] = {0};
Upvotes: 0