global variables initialization

Discussion in 'C Programming' started by paresh, Nov 23, 2008.

  1. paresh

    paresh Guest

    When you declare some global variables(any type), do they initialized
    with zero implicitly or they have garbage (whatever was there on the
    memory location)?
    -Paresh
    paresh, Nov 23, 2008
    #1
    1. Advertising

  2. paresh <> writes:

    > When you declare some global variables(any type), do they initialized
    > with zero implicitly or they have garbage (whatever was there on the
    > memory location)?


    Zero. It says so in 6.7.8 (10) of the C99 standard.
    Nate Eldredge, Nov 23, 2008
    #2
    1. Advertising

  3. paresh wrote:
    > When you declare some global variables(any type), do they initialized
    > with zero implicitly or they have garbage (whatever was there on the
    > memory location)?


    All objects with static storage duration are zero-initialized (unless
    they have a user-supplied initializer). That includes the global variables.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 23, 2008
    #3
  4. paresh

    Flash Gordon Guest

    Richard Heathfield wrote, On 23/11/08 09:32:
    > Nate Eldredge said:
    >
    >> paresh <> writes:
    >>
    >>> When you declare some global variables(any type), do they initialized
    >>> with zero implicitly or they have garbage (whatever was there on the
    >>> memory location)?

    >> Zero. It says so in 6.7.8 (10) of the C99 standard.

    >
    > What if he isn't using C99? (Hardly anyone is.)


    Then it is still initialised to an appropriate zero as this has not been
    changed since C89.
    --
    Flash Gordon
    If spamming me sent it to
    If emailing me use my reply-to address
    See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
    Flash Gordon, Nov 23, 2008
    #4
  5. Flash Gordon wrote:
    > Richard Heathfield wrote, On 23/11/08 09:32:
    >> Nate Eldredge said:
    >>
    >>> paresh <> writes:
    >>>
    >>>> When you declare some global variables(any type), do they
    >>>> initialized with zero implicitly or they have garbage (whatever
    >>>> was there on the memory location)?
    >>> Zero. It says so in 6.7.8 (10) of the C99 standard.

    >>
    >> What if he isn't using C99? (Hardly anyone is.)

    >
    > Then it is still initialised to an appropriate zero as this has not
    > been changed since C89.


    And I'm pretty sure Richard knows this. I'm unsure though why he didn't
    mention it...

    Bye, Jojo
    Joachim Schmitz, Nov 23, 2008
    #5
  6. Richard Heathfield wrote:
    > Joachim Schmitz said:
    >
    >> Flash Gordon wrote:
    >>> Richard Heathfield wrote, On 23/11/08 09:32:
    >>>> Nate Eldredge said:
    >>>>
    >>>>> paresh <> writes:
    >>>>>
    >>>>>> When you declare some global variables(any type), do they
    >>>>>> initialized with zero implicitly or they have garbage (whatever
    >>>>>> was there on the memory location)?
    >>>>> Zero. It says so in 6.7.8 (10) of the C99 standard.
    >>>>
    >>>> What if he isn't using C99? (Hardly anyone is.)
    >>>
    >>> Then it is still initialised to an appropriate zero as this has not
    >>> been changed since C89.

    >>
    >> And I'm pretty sure Richard knows this. I'm unsure though why he
    >> didn't mention it...

    >
    > Perhaps because I'd already answered the OP's question elsethread?
    > Could that be it?


    Of course it could be that (and yes you had and no I didn't notice in time),
    but would it have harmed to mention it here too?

    Bye, Jojo
    Joachim Schmitz, Nov 23, 2008
    #6
  7. Richard Heathfield <> writes:

    > Nate Eldredge said:
    >
    >> paresh <> writes:
    >>
    >>> When you declare some global variables(any type), do they initialized
    >>> with zero implicitly or they have garbage (whatever was there on the
    >>> memory location)?

    >>
    >> Zero. It says so in 6.7.8 (10) of the C99 standard.

    >
    > What if he isn't using C99? (Hardly anyone is.)


    It also says so in 3.5.7 of the ANSI C90 standard (numbered based on
    http://flash-gordon.me.uk/ansi.c.txt), and in A.8.6 of K&R I, if it
    comes to that.

    Better?
    Nate Eldredge, Nov 23, 2008
    #7
  8. paresh

    osmium Guest

    "Joachim Schmitz" wrote:

    > Richard Heathfield wrote:
    >> Joachim Schmitz said:
    >>
    >>> Flash Gordon wrote:
    >>>> Richard Heathfield wrote, On 23/11/08 09:32:
    >>>>> Nate Eldredge said:
    >>>>>
    >>>>>> paresh <> writes:
    >>>>>>
    >>>>>>> When you declare some global variables(any type), do they
    >>>>>>> initialized with zero implicitly or they have garbage (whatever
    >>>>>>> was there on the memory location)?
    >>>>>> Zero. It says so in 6.7.8 (10) of the C99 standard.
    >>>>>
    >>>>> What if he isn't using C99? (Hardly anyone is.)
    >>>>
    >>>> Then it is still initialised to an appropriate zero as this has not
    >>>> been changed since C89.
    >>>
    >>> And I'm pretty sure Richard knows this. I'm unsure though why he
    >>> didn't mention it...

    >>
    >> Perhaps because I'd already answered the OP's question elsethread?
    >> Could that be it?

    >
    > Of course it could be that (and yes you had and no I didn't notice in
    > time), but would it have harmed to mention it here too?


    Richard much prefers precision to clarity. This is simply another example
    of that preference.
    osmium, Nov 23, 2008
    #8
  9. paresh wrote:
    > When you declare some global variables(any type), do they initialized
    > with zero implicitly or they have garbage (whatever was there on the
    > memory location)?


    Any static variable (which includes variables of file scope) are
    initialized to zero. However, a block scope static variables are
    guaranteed initialization only if the block is entered at its beginning.

    Local variables must be explicitly initialized or have values assigned
    to them before being read.

    "Global" occurs three times in the standard and not in contexts
    referring to storage duration (extent, lifetime), scope (visibilty), or
    linkage. Not only is this not a term used in C, it is inherently
    ambiguous. For example, do you mean that it is "global" within a
    translation unit (file scope, internal linkage) or across all
    translation units which will be linked (file scope, external linkage)?
    Martin Ambuhl, Nov 23, 2008
    #9
  10. Martin Ambuhl <> writes:
    > paresh wrote:
    >> When you declare some global variables(any type), do they initialized
    >> with zero implicitly or they have garbage (whatever was there on the
    >> memory location)?

    >
    > Any static variable (which includes variables of file scope) are
    > initialized to zero.


    Agreed.

    > However, a block scope static variables are
    > guaranteed initialization only if the block is entered at its
    > beginning.


    I don't think that's correct. I think that static variables are
    guaranteed to be initialized regardless of how the enclosing block is
    entered. Your statement applies to automatic variables, and perhaps
    that's what you meant.

    A concrete example:

    #include <stdio.h>
    int main(void)
    {
    goto LABEL;
    {
    static int x = 10;
    int y = 20;
    LABEL:
    printf("x = %d\n", x);
    printf("y = %d\n", y);
    }
    return 0;
    }

    x is guaranteed to have the value 10; y's value is indeterminate.


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 23, 2008
    #10
  11. paresh

    Guest

    On Nov 23, 2:51 pm, Richard Heathfield <> wrote:
    > osmium said:
    > > "Joachim Schmitz" wrote:
    > >> Richard Heathfield wrote:
    > >>> Joachim Schmitz said:
    > >>>> Flash Gordon wrote:
    > >>>>> Richard Heathfield wrote, On 23/11/08 09:32:

    > <snip>
    > >>>>>> What if he isn't using C99? (Hardly anyone is.)

    >
    > >>>>> Then it is still initialised to an appropriate zero as this has not
    > >>>>> been changed since C89.

    >
    > >>>> And I'm pretty sure Richard knows this. I'm unsure though why he
    > >>>> didn't mention it...

    >
    > >>> Perhaps because I'd already answered the OP's question elsethread?
    > >>> Could that be it?

    >
    > >> Of course it could be that (and yes you had and no I didn't notice in
    > >> time), but would it have harmed to mention it here too?

    >
    > > Richard much prefers precision to clarity.

    >
    > Actually, I prefer both, if that's possible.


    If you really preferred either precision or clarity, you wouldn't have
    made the parenthetical remark about C99. This is c.l.c, after all,
    where people don't pay attention to anything but what the ISO C
    standards have to say, and as far as ISO is concerned, C90 is
    obsolete.

    Sebastian
    , Nov 23, 2008
    #11
  12. paresh

    CBFalconer Guest

    paresh wrote:
    >
    > When you declare some global variables(any type), do they
    > initialized with zero implicitly or they have garbage (whatever
    > was there on the memory location)?


    There is no such thing as a 'global variable' in C. If you mean a
    'static variable', that exists, and should always be zero
    initialized unless given another initialization value.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Nov 24, 2008
    #12
  13. paresh

    CBFalconer Guest

    Richard Heathfield wrote:
    > Nate Eldredge said:
    >> paresh <> writes:
    >>
    >>> When you declare some global variables(any type), do they
    >>> initialized with zero implicitly or they have garbage (whatever
    >>> was there on the memory location)?

    >>
    >> Zero. It says so in 6.7.8 (10) of the C99 standard.

    >
    > What if he isn't using C99? (Hardly anyone is.)


    Then the C99 standard gives him a good idea. If he can find the
    C89 (or C90) standard he can check it. Why criticize a 'best
    available' answer?

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Nov 24, 2008
    #13
  14. Keith Thompson <> wrote:
    > Martin Ambuhl <> writes:
    > > paresh wrote:
    > > > When you declare some global variables(any type), do
    > > > they initialized with zero implicitly or they have
    > > > garbage (whatever was there on the memory location)?

    > >
    > > Any static variable (which includes variables of file
    > > scope) are initialized to zero.

    >
    > Agreed.
    >
    > > However, a block scope static variables are guaranteed
    > > initialization only if the block is entered at its
    > > beginning.

    >
    > I don't think that's correct.  I think that static
    > variables are guaranteed to be initialized regardless
    > of how the enclosing block is entered.


    Indeed. Execution Environments (2.1.2 C89 draft, 5.1.2p1
    N1256):

    ... All objects in static storage shall be initialized
    (set to their initial values) before program startup. ...

    --
    Peter
    Peter Nilsson, Nov 24, 2008
    #14
  15. Richard Heathfield <> writes:

    > osmium said:
    >
    >> "Joachim Schmitz" wrote:
    >>
    >>> Richard Heathfield wrote:
    >>>> Joachim Schmitz said:
    >>>>
    >>>>> Flash Gordon wrote:
    >>>>>> Richard Heathfield wrote, On 23/11/08 09:32:

    > <snip>
    >>>>>>>
    >>>>>>> What if he isn't using C99? (Hardly anyone is.)
    >>>>>>
    >>>>>> Then it is still initialised to an appropriate zero as this has not
    >>>>>> been changed since C89.
    >>>>>
    >>>>> And I'm pretty sure Richard knows this. I'm unsure though why he
    >>>>> didn't mention it...
    >>>>
    >>>> Perhaps because I'd already answered the OP's question elsethread?
    >>>> Could that be it?
    >>>
    >>> Of course it could be that (and yes you had and no I didn't notice in
    >>> time), but would it have harmed to mention it here too?

    >>
    >> Richard much prefers precision to clarity.

    >
    > Actually, I prefer both, if that's possible.


    An admirable objective but asking a question that raises (in the minds
    of the inexperienced) the idea that there might be a difference here
    between C90 and C99 is hardly you at your most clear!

    > But I also prefer brevity,
    > where it's consonant with precision and clarity. I see no point in
    > repeating in one part of the thread something that I have already said in
    > another part thereof.


    The clarification (that all is well even if the OP is not using C99)
    suggested by Joachim Schmitz was not there in you original very clear
    reply, so I don't see how it would have been repetitious to have made
    it either instead of, or in answer to, the question you asked.

    > (That's not to say that I have a perfect record in
    > avoiding repetetetetetition within a thread...)


    At first I though it was an attempt to strike up another C90/C99 mega
    thread. Surely I am mistaken about that?

    --
    Ben.
    Ben Bacarisse, Nov 24, 2008
    #15
  16. Keith Thompson wrote:

    >> However, a block scope static variables are
    >> guaranteed initialization only if the block is entered at its
    >> beginning.

    >
    > I don't think that's correct. I think that static variables are
    > guaranteed to be initialized regardless of how the enclosing block is
    > entered. Your statement applies to automatic variables, and perhaps
    > that's what you meant.


    Thank you for the correction.
    Martin Ambuhl, Nov 24, 2008
    #16
  17. paresh

    CBFalconer Guest

    Richard Heathfield wrote:
    > CBFalconer said:
    >> Richard Heathfield wrote:
    >>> Nate Eldredge said:
    >>>> paresh <> writes:
    >>>>
    >>>>> When you declare some global variables(any type), do they
    >>>>> initialized with zero implicitly or they have garbage
    >>>>> (whatever was there on the memory location)?
    >>>>
    >>>> Zero. It says so in 6.7.8 (10) of the C99 standard.
    >>>
    >>> What if he isn't using C99? (Hardly anyone is.)

    >>
    >> Then the C99 standard gives him a good idea. If he can find the
    >> C89 (or C90) standard he can check it. Why criticize a 'best
    >> available' answer?

    >
    > What makes you think it was a "best available" answer?


    The fact that most can't buy, or download, a reasonable copy of the
    C90 standard.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Nov 24, 2008
    #17
  18. paresh

    paresh Guest

    On Nov 23, 2:11 pm, paresh <> wrote:
    > When you declare some global variables(any type), do they initialized
    > with zero implicitly or they have garbage (whatever was there on the
    > memory location)?
    > -Paresh


    I meant "define"!
    paresh, Nov 24, 2008
    #18
  19. paresh

    James Kuyper Guest

    CBFalconer wrote:
    > Richard Heathfield wrote:

    ....
    >> What makes you think it was a "best available" answer?

    >
    > The fact that most can't buy, or download, a reasonable copy of the
    > C90 standard.


    I think you mean "reasonably priced"?
    James Kuyper, Nov 24, 2008
    #19
  20. paresh

    James Kuyper Guest

    Richard Heathfield wrote:
    > CBFalconer said:
    >
    >> Richard Heathfield wrote:
    >>> CBFalconer said:

    ....
    >>>> Then the C99 standard gives him a good idea. If he can find the
    >>>> C89 (or C90) standard he can check it. Why criticize a 'best
    >>>> available' answer?
    >>> What makes you think it was a "best available" answer?

    >> The fact that most can't buy, or download, a reasonable copy of the
    >> C90 standard.

    >
    > But most people don't program in C99. Quoting from a document that doesn't
    > actually describe the language being used seems like a very strange
    > definition of "best available".


    First of all, keep in mind that "best" isn't necessarily "good". The
    best available way of dealing with spam, for instance, isn't a good way
    of dealing with it, it's just better than the other ways.

    But more directly to the point, the C99 standard does describe the C90
    language - with only a relatively few errors. If you keep in ming the
    summary of the changes from C90 in paragraph 5 of the Foreword, and
    avoid discussing issues related to those changes, you're not going to
    make many significant errors using the C99 standard to inform
    discussions about C90.

    It would be better to make no errors, certainly - but is that difference
    important enough to justify paying for a legitimate copy of the C90
    standard? I don't know about you, but I certainly have better uses for
    my money.
    James Kuyper, Nov 24, 2008
    #20
    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. Wayne
    Replies:
    2
    Views:
    456
    Wayne
    Nov 11, 2003
  2. JKop
    Replies:
    10
    Views:
    923
  3. Rahul Gandhi

    Initialization of global variables

    Rahul Gandhi, Feb 2, 2004, in forum: C Programming
    Replies:
    3
    Views:
    543
    Dan Pop
    Feb 2, 2004
  4. jubelbrus
    Replies:
    5
    Views:
    601
    JohnQ
    Jul 20, 2007
  5. Milan Radovich
    Replies:
    45
    Views:
    2,764
    Jorgen Grahn
    Apr 21, 2010
Loading...

Share This Page