473,511 Members | 16,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on Static Global variable

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
4 1668
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
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
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 (40°39.22'N, 111°50.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
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 (40°39.22'N, 111°50.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6348
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
3
3918
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
4
343
by: Diffident | last post by:
Hello All, I have a question which is pertinent to Page's lifecycle. I declared a protected static object (global variable within that class) whose value is set only once when the page is...
3
2962
by: cr113 | last post by:
I'm switching over from VB and I have a question. Suppose I had a VB project with 2 forms and one variable. Call them Form1,Form2 and strUsername. Suppose I want to initialize strUsername to...
9
8620
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
10
2633
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
10
3554
by: ma | last post by:
Hello, I want to create a global class. To do this I did the followings: 1- Create a class name test. It has a public variable named mystring. public class test { public string mystring =...
14
360
by: cs1975 | last post by:
Hi Everyone, I wanted to make a global variable double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; to static global static double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; I am updating this global...
12
2996
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.