By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

Question on Static Global variable

P: n/a
Question#1:
==========
==========
What is the default value for any Static Global variable?

Example:

A.c:
=====

static int i;

Does "i" get to initialized to "0"?

Question#2:
==========
==========
Can I access a Static Global variable from a different file as given in
the following example?

Example:
A.c:
=====

static int i;

int * foo(void)
{
return &i;
}
B.c:
====
int *a = foo();
In the above code, is it valid to make a call "int *a = foo();" in B.c?
What happens when I try it?

Jul 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
C_Programmer wrote:
Question#1:
What is the default value for any Static Global variable?
http://c-faq.com/decl/initval.html
>
Question#2:
Can I access a Static Global variable from a different file as given in
the following example?

Example:
A.c:
static int i;
int * foo(void){return &i;}

B.c:
int *a = foo();
B.c should have a prototype for foo, and it should
be declared as extern, and you'll need to make sure
the definition of foo is available at link time.
In the above code, is it valid to make a call "int *a = foo();" in B.c?
What happens when I try it?
I know what happens when I try it. If you try it, then you
will know what happens when you try it. Experimentation
is the key to learning. Contrary to certain claims, you
don't actually need to worry about demons flying out
of your nose if you do something wrong.

Jul 12 '06 #2

P: n/a
In article <11**********************@h48g2000cwc.googlegroups .com>,
C_Programmer <he****@yahoo.comwrote:
>Question#1:
==========
==========
What is the default value for any Static Global variable?
C89 3.5.7
If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member
that has arithmetic type were assigned 0 and every member
that has pointer type were assigned a null pointer constant.

>Question#2:
==========
==========
Can I access a Static Global variable from a different file as given in
the following example?
>A.c:
=====
>static int i;

int * foo(void)
{
return &i;
}
>B.c:
====
int *a = foo();
>In the above code, is it valid to make a call "int *a = foo();" in B.c?
Yes, provided you have a proper prototype in scope, and the above
code fragment was inside a function.

>What happens when I try it?
The address of A's i will be stored in B's a .

C pointers work by storage addresses, not by name, so if in B you had

int i = 42;
int *a = foo();

then the i from B would *not* interfere in any way with the i from A.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Jul 12 '06 #3

P: n/a
In article <11**********************@i42g2000cwa.googlegroups .com>
Bill Pursell <bi**********@gmail.comwrote:
>I know what happens when I try it. If you try it, then you
will know what happens when you try it. Experimentation
is the key to learning. Contrary to certain claims, you
don't actually need to worry about demons flying out
of your nose if you do something wrong.
Experimentation is all well and good, but it has its limits. For
instance, what happens if you experimentally try driving through
red stop-lights? (It often works, but I do not recommend it.) [%]

(Bill Pursell already gave the correct answer to the first
question, and someone else already gave the correct answer to
the second, so I have snipped those.)

[% There is a joke about someone out in the country driving around,
zooming through all the red lights. His passenger, a city girl,
is understandably terrified. He keeps telling her: "Don't worry,
my brother taught me to drive!" Then he comes across a green
light, and screeches to a stop.

"What are you doing?" she asks. "You went screaming through all
the red lights, why are you stopping at this green one?"

"Are you kidding? My brother might be out there driving!"]

Experimentation will show you what *your* compiler does, at least
with whatever settings you chose. Any change to anything, however,
might change the result, if the effect of whatever construct you
is undefined:

int *p = (int *)0x40002078;
*p = 42;

This code fragment "works" on some machines, but probably not on
yours.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 13 '06 #4

P: n/a
Thanks for all the posts. I ran these on my system.
I was kind of expecting compiler errors or warnings where I used foo()
in B.c,
but as it explained in Walter Roberson's response it did not.

Walter: Could you please explain me what do you mean by "C pointers
work by storage addresses, not by name".

Chris Torek: I tried the lines that u posted. It did not give any
compilation warnings, but when I ran
it gave me segmentation fault. is it because the address location
"0x40002078" may not be valid for my
system?

Thanks.

Chris Torek wrote:
In article <11**********************@i42g2000cwa.googlegroups .com>
Bill Pursell <bi**********@gmail.comwrote:
I know what happens when I try it. If you try it, then you
will know what happens when you try it. Experimentation
is the key to learning. Contrary to certain claims, you
don't actually need to worry about demons flying out
of your nose if you do something wrong.

Experimentation is all well and good, but it has its limits. For
instance, what happens if you experimentally try driving through
red stop-lights? (It often works, but I do not recommend it.) [%]

(Bill Pursell already gave the correct answer to the first
question, and someone else already gave the correct answer to
the second, so I have snipped those.)

[% There is a joke about someone out in the country driving around,
zooming through all the red lights. His passenger, a city girl,
is understandably terrified. He keeps telling her: "Don't worry,
my brother taught me to drive!" Then he comes across a green
light, and screeches to a stop.

"What are you doing?" she asks. "You went screaming through all
the red lights, why are you stopping at this green one?"

"Are you kidding? My brother might be out there driving!"]

Experimentation will show you what *your* compiler does, at least
with whatever settings you chose. Any change to anything, however,
might change the result, if the effect of whatever construct you
is undefined:

int *p = (int *)0x40002078;
*p = 42;

This code fragment "works" on some machines, but probably not on
yours.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jul 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.