473,671 Members | 2,252 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 1672
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){retur n &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************ **********@h48g 2000cwc.googleg roups.com>,
C_Programmer <he****@yahoo.c omwrote:
>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************ **********@i42g 2000cwa.googleg roups.com>
Bill Pursell <bi**********@g mail.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************ **********@i42g 2000cwa.googleg roups.com>
Bill Pursell <bi**********@g mail.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
6358
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 keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
3
3928
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; } /* in above case where is variable k and j mapped in memory layout ? */
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 loaded. Will that object's value be accessible during consequent postbacks or is it set to null after HTML is rendered initially?
3
2969
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 "jones" and use this value in Form1 and Form2 at any time. In VB I would create a module with the following code: Module test Public strUsername As String
9
8646
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 Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
10
2650
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
3573
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 = "hello world";
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 variable in one of the methods in the flow,
12
3010
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. The global variables and global functions are hidden to prevent from accessing by the programmers. All global functions share global variables. Only very few global functions are allowed to be reusability for the programmers to use. Few...
0
8393
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6229
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5696
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.