global and static declaration

T

the_init

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;
}
 
G

Guest

the_init said:
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.
 
E

Eric Sosman

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

Fred Kleinschmidt

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

Mark McIntyre

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
 

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,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top