K
Keith
I've got this bit of code, and I can't really explain to myself why it does what it does....
int
func1()
{
static int r=0;
return(++r);
}
int
func2()
{
static int r;
r = 0;
return(++r);
}
int
main()
{
printf("%d,%d,%d\n", func1(), func1(), func1());
printf("%d,%d,%d\n", func2(), func2(), func2());
exit(0);
}
I imagined it would print
1,1,1
1,1,1
but after compiling and running, it prints
3,2,1
1,1,1
In func1, are the static r and the returned r not the same var? what's what?
Even it the returned r is a global, why does it display 3,2,1 instead of 1,2,3?
int
func1()
{
static int r=0;
return(++r);
}
int
func2()
{
static int r;
r = 0;
return(++r);
}
int
main()
{
printf("%d,%d,%d\n", func1(), func1(), func1());
printf("%d,%d,%d\n", func2(), func2(), func2());
exit(0);
}
I imagined it would print
1,1,1
1,1,1
but after compiling and running, it prints
3,2,1
1,1,1
In func1, are the static r and the returned r not the same var? what's what?
Even it the returned r is a global, why does it display 3,2,1 instead of 1,2,3?