Thread: Automatic variables default intial value

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    32

    Automatic variables default intial 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.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    32
    I actually meant auto type variables (variables store values automatically) .Thanks for explanation though.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    K&R second edition page 31 : 'Each local variable ... such variables are usually known as automatic variables, following terminology in other languages'

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Why the default initial value of automatic variables are garbage values?
    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.

    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.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by chibi.coder View Post
    Why the default initial value of automatic variables are garbage values? Why not 0 like static and global variables initial value?
    Quite simply because C & C++ have the motto that you don't pay for what you don't use.

    If you don't need an initial value of zero, then you don't pay the performance penalty of having the language do so unnecessarily.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Automatic variables in external structure definition
    By Boxknife in forum C Programming
    Replies: 2
    Last Post: 06-23-2008, 12:56 PM
  2. Static vs. Automatic Variables
    By Trekkie in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:55 PM
  3. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 07:14 AM
  4. Replies: 9
    Last Post: 10-26-2005, 07:29 AM
  5. automatic variables
    By FOOTOO in forum C Programming
    Replies: 5
    Last Post: 03-08-2005, 06:30 PM