Initial values of File scoped and Block level variables

Discussion in 'C Programming' started by Madhav, Jan 6, 2006.

  1. Madhav

    Madhav Guest

    Hi all,

    I did not understand why do the global vars are
    initialized to NULL where as the block level variables have random
    values? I know that the C standard requires this as was mentioned in a
    recent thread.

    I want to know why this descrimination is in place. Can't all the
    variables be initialised to NULL automatically by the compiler? This
    would make programming a little easier.

    Regards,
    Madhav.
     
    Madhav, Jan 6, 2006
    #1
    1. Advertising

  2. Madhav

    jacob navia Guest

    Madhav a écrit :
    > Hi all,
    >
    > I did not understand why do the global vars are
    > initialized to NULL where as the block level variables have random
    > values? I know that the C standard requires this as was mentioned in a
    > recent thread.
    >
    > I want to know why this descrimination is in place. Can't all the
    > variables be initialised to NULL automatically by the compiler? This
    > would make programming a little easier.
    >
    > Regards,
    > Madhav.
    >


    The lcc-win32 compiler will do exactly what you want if you call it
    with the command line switch:
    lcc -stackinit 0

    This is a common need that can be efficiently implemented in the
    compiler instead of doing it manually.

    jacob
     
    jacob navia, Jan 6, 2006
    #2
    1. Advertising

  3. Madhav a écrit :
    >
    > I did not understand why do the global vars are
    > initialized to NULL where as the block level variables have random
    > values?


    Because it is the way the C-language s defined.

    > I know that the C standard requires this as was mentioned in a
    > recent thread.


    So, what more ?

    > I want to know why this descrimination is in place.


    Not a C question. It's a language designer question.

    Can't all the
    > variables be initialised to NULL automatically by the compiler? This


    you probably meant 0.

    > would make programming a little easier.


    It's not certain... A good practice is to define and initialize on the
    same statement.

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 7, 2006
    #3
  4. Emmanuel Delahaye <> writes:
    > Madhav a écrit :
    >> I did not understand why do the global vars are
    >> initialized to NULL where as the block level variables have random
    >> values?

    >
    > Because it is the way the C-language s defined.
    >
    >> I know that the C standard requires this as was mentioned in a
    >> recent thread.

    >
    > So, what more ?
    >
    >> I want to know why this descrimination is in place.

    >
    > Not a C question. It's a language designer question.


    I disagree. I think that discussions about why C is the way it is are
    topical. An understanding beyond "because the Standard says so" can
    be helpful.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 7, 2006
    #4
  5. In article <>,
    Keith Thompson <allegedly, > appears to have written:
    ....
    >I disagree. I think that discussions about why C is the way it is are
    >topical. An understanding beyond "because the Standard says so" can
    >be helpful.


    Uh, oh. It looks like Keith's account has been hacked.
     
    Kenny McCormack, Jan 8, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish

    block scoped variables

    Andy Fish, Aug 15, 2003, in forum: Java
    Replies:
    3
    Views:
    422
    Chris Uppal
    Aug 16, 2003
  2. pabbu
    Replies:
    8
    Views:
    732
    Marc Boyer
    Nov 7, 2005
  3. Madhav
    Replies:
    27
    Views:
    568
    Dave Thompson
    Jan 16, 2006
  4. Pavel Smerk
    Replies:
    1
    Views:
    127
    Logan Capaldo
    Aug 3, 2006
  5. Replies:
    3
    Views:
    114
Loading...

Share This Page