Possible Duplicate:
Why does the C standard leave use of indeterminate variables undefined?
When a variable or pointer is not initialized why it holds a garbage value, is it some kind of a necessity for these entities to hold some sort of value to exist in the memory?
Only local variables are not initialized in C. Global variables or static ones are initialized.
The reason of not initializing them in the language specification is (or was) performance : the compiler don’t need to emit machine code for implicit initialization of local values on the call stack. (in contrast, Java initializes all local variables)
My belief is that you should almost always initialize them (e.g. to 0 for integers, and to NULL for pointers; for aggregate like local arrays and structures, use
memset). A good compiler would optimize almost always unneeded initialization. And if all your local variables are initialized, your code has a more reproducible behavior.The GCC compiler (at least the recent versions e.g. 4.6) gives good warning about unitialized variables. I strongly suggest passing
-Wallto GCC.