Why the default initial value of automatic variables are garbage values? Why not 0 like static and global variables initial value?
Why the default initial value of automatic variables are garbage values? Why not 0 like static and global variables initial value?
Last edited by chibi.coder; 12-19-2011 at 03:12 PM.
C has no "automatic" variables...
Global variables are declared at compile time and set to 0
Function-local variables are declared on the stack and are not initialized to any set value... so you get to do it yourself.
This should present no problem to a careful programmer.
I actually meant auto type variables (variables store values automatically) .Thanks for explanation though.
K&R second edition page 31 : 'Each local variable ... such variables are usually known as automatic variables, following terminology in other languages'
Probably because that could impose a penalty each time a function is entered. Zeroing values at startup is not difficult (even if it takes time it only happens once), but automatic variables are created each time a function starts, so you'd be creating overhead every time you zero them out. C generally takes the approach of making the implementor's life easy (and possible) rather than the programmer's life, so C can run everywhere.Why the default initial value of automatic variables are garbage values?
It's also in part, I suspect, because that's just how it was done on Unix and the original C standard tended toward codifying existing behavior.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"