Why might the values in an array declared in C have values that were not set explicitly?
-
My understanding is that for those array did not assign any value, the result should appear as 0's. However, in this case (the following code), all uninitialized indexes appear non-zero values. Why this happened? and how to solve this problem? The source of the code is from program 7.1 in book, Programming in C . The result: (~): clang file.c -std=c99 -Wall -Werror -fcolor-diagnostics -lcs50 -lm (~):./a.out values[0] = 197 values[1] = 134518668 values[2] = -101 values[3] = 547 values[4] = -1074789596 values[5] = 350 values[6] = -1612950947 values[7] = -1611381820 values[8] = 4096 values[9] = 35 The code: #include <stdio.h> #include <cs50.h> int main(int argc, string argv[]) { 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; }
-
Answer:
Here's a similar question from StackOverflow with some well explained answers. http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value PS: I think StackOverflow is more suited than Quora for this type of question.
Vinay Kola at Quora Visit the source
Other answers
Well, the automatic variables in C are not initialized at all by default. The only exception to this - if you are initializing at least one element of an array, the rest of it gets initialized to zero. The same zeroing effect cannot be got by assignment which is what you are doing down the lane. Hence, the garbage values, which can be 0 by coincidence! :) Also, note that automatic variables are just local variables within a block of a function, but that doesn't mean the reverse should be true. Heard of static variables declared and defined within a function? Yeah, the desirable effect of zeroing down all the array elements by not even initializing one of them is possible having a static array declared either within any function or outside of all the functions(in this case you wouldn't need the keyword static which would otherwise become a file-scope variable). And these will occupy different segment in memory unlike the automatic ones being on stack. Here is an ex: int c[ 40 ]; // Zeroed down by default. int main(void) { int a[ 20 ] = { 20 }; // Zeros down rest. static int b[ 30 ]; // Zeroed down by default. int d[ 50 ]; // Contains garbage by default. d[ 0 ] = 12; // Still the rest of the array d will be having garbage values // as the assignment operation in C is not meant for zeroing // down the rest of the elements of the array - it's just the way // "C" is designed. return 0; } Hope this helps. Cheers, Raghavan
Raghavan Santhanam
Related Q & A:
- Why generally to find a Euler cycle is easier than Hamilton cycle for the same set of nodes?Best solution by Computer Science
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- Converting char array to double in C?Best solution by stackoverflow.com
- Why might a unitary government be more successful in a country like Japan than the US?Best solution by chacha.com
- Why might assimilation not be achieved in a capitalist society?Best solution by encyclopedia.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.