473,473 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

global and static declaration

Hi friends,

Can you please explain me why the following code prints 10, instead of
20 - Here I am little confused with the static and global declaration
of i in the same program.
#include<stdio.h>

int i;

int foo();
int print();

int main(void)
{
i = 10;
foo();
return 0;
}

int foo()
{
static int i = 20;
print();
return 0;
}

int print()
{
printf("%d", i);
return 0;
}

Feb 12 '07 #1
4 7091
the_init wrote:
Hi friends,

Can you please explain me why the following code prints 10, instead of
20 - Here I am little confused with the static and global declaration
of i in the same program.
#include<stdio.h>

int i;
This declares one i, let's call it i [1].
int foo();
int print();

int main(void)
{
i = 10;
Since only i [1] is in scope, this sets i [1] to 10.
foo();
return 0;
}

int foo()
{
static int i = 20;
This declares a different i, let's call it i [2].
print();
return 0;
}
Here i [2] goes out of scope.
int print()
{
printf("%d", i);
Since i [2] is out of scope, this prints i [1].
return 0;
}
The thing to remember is that in C, which variable is used is
determined at compile time, not at run time as in some other languages.

Feb 12 '07 #2
the_init wrote On 02/12/07 13:37,:
Hi friends,

Can you please explain me why the following code prints 10, instead of
20 - Here I am little confused with the static and global declaration
of i in the same program.
#include<stdio.h>

int i;
This declaration of `i' is "visible" from here to the
end of the module, except where "hidden" by another
declaration of `i'.
int foo();
int print();

int main(void)
{
i = 10;
Only one declaration of `i' has been seen so far, so
this reference is to that `i', namely, the `i' declared
at file scope.
foo();
return 0;
}

int foo()
{
static int i = 20;
This declaration of `i' is visible from here to the
end of its containing block, namely, the end of the function
foo(). In that scope, it "hides" or "shadows" the file-scope
declaration of `i': `i' now refers to this inner variable and
there is no name that can refer to the file-scope variable.
print();
return 0;
}
At the end of foo(), the inner declaration of `i' goes
out of scope, and `i' regains its former meaning: it now
refers to the file-scope variable.
int print()
{
printf("%d", i);
The only visible declaration of `i' is the one for the
file-scope variable, so that is the `i' whose value is printed.
return 0;
}
--
Er*********@sun.com
Feb 12 '07 #3

"the_init" <th******@gmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
Hi friends,

Can you please explain me why the following code prints 10, instead of
20 - Here I am little confused with the static and global declaration
of i in the same program.
#include<stdio.h>

int i;

int foo();
int print();

int main(void)
{
i = 10;
foo();
return 0;
}

int foo()
{
static int i = 20;
The above variable "i" is visible only to function foo();

Calling "print" here leaves function foo. foo's
local variables are not visible outside foo.
print();
return 0;
}

int print()
{
printf("%d", i);
The global "i" is in scope here - that is the only "i" that
print() knows about. Its value is 10, set in main.
return 0;
}
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Feb 12 '07 #4
On 12 Feb 2007 10:37:27 -0800, in comp.lang.c , "the_init"
<th******@gmail.comwrote:
>int foo()
{
static int i = 20;
this definition of "i" is only visible inside the function foo(). Not
even functions called by foo() can see it.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
8
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
10
by: Jay Wolfe | last post by:
Hello, I'm trying to make sure I use best practices (and hence save myself some headaches) with the declaration and definition of global variables. Let's say I have an app with 30 files,...
23
by: Anil Gupte | last post by:
I am trying to set up a function that connects to the database that I can then use gloablly. I set up a class called L3Global in which I have a function as follows: Public Function...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
1
by: Jaco Naude | last post by:
Hi, I'm using a static library in my application which links fine except for a few global variables. The static library only contains a bunch of .cpp and .h files and the global variables are...
0
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
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
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
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.