global variables initialization

P

paresh

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
 
N

Nate Eldredge

paresh said:
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.
 
A

Andrey Tarasevich

paresh said:
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.
 
F

Flash Gordon

Richard Heathfield wrote, On 23/11/08 09:32:
Nate Eldredge said:


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.
 
J

Joachim Schmitz

Flash said:
Richard Heathfield wrote, On 23/11/08 09:32:

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
 
J

Joachim Schmitz

Richard said:
Joachim Schmitz said:


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
 
O

osmium

Joachim Schmitz said:
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.
 
M

Martin Ambuhl

paresh said:
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)?
 
K

Keith Thompson

Martin Ambuhl said:
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.
 
S

s0suk3

osmium said:


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
 
C

CBFalconer

paresh said:
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.
 
C

CBFalconer

Richard said:
Nate Eldredge said:

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?
 
P

Peter Nilsson

Keith Thompson said:
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. ...
 
B

Ben Bacarisse

Richard Heathfield said:
osmium said:


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?
 
M

Martin Ambuhl

Keith said:
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.
 
C

CBFalconer

Richard said:
CBFalconer said:

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.
 
P

paresh

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"!
 
J

James Kuyper

CBFalconer said:
Richard Heathfield wrote: ....

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

I think you mean "reasonably priced"?
 
J

James Kuyper

Richard said:
CBFalconer said:


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top