Static variable



 DEVELOP > c-Plus-Plus > Static variable

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Stefan Istrate"
Date: 31 Jan 2008 05:24:02 AM
Object: Static variable
I have the following code and I don't understand why is printed 0 0 1
4 4.
#include <stdio.h>
int sum(int max)
{
int i; static int s = 0;
for (i=s; i<max; i++)
s+=i;
return s;
}
int main()
{
int j, n= 5;
for (j=0; j<n; j++)
printf("%3d",sum(j));
return 0;
}
Could anyone help me and explain me why this happens? I saw that the
static variable create this behaviour.
Thank you!
.

User: "Michael DOUBEZ"

Title: Re: Static variable 31 Jan 2008 06:06:58 AM
Stefan Istrate a écrit :

I have the following code and I don't understand why is printed 0 0 1
4 4.

#include <stdio.h>

int sum(int max)
{
int i; static int s = 0;
for (i=s; i<max; i++)
s+=i;
return s;
}

Your function is equivalent to:
int sum(int max)
{
static int s = 0;
if(s<max)
{
s+=(max-s)*(max+s-1)/2
}
return s;
}


int main()
{
int j, n= 5;
for (j=0; j<n; j++)
printf("%3d",sum(j));
return 0;
}

Could anyone help me and explain me why this happens? I saw that the
static variable create this behaviour.

The iterations bring the following transformation
s=0
max=i=0 => s unmodified -> return 0
max=i=1 => s=1*0/2=0 -> return 0
max=i=2 => s=2*(2-1)/2=1 -> return 1
max=i=3 => s=(3-2)*(5-1)/2=4 -> return 4
max=i=4 => s unmodified -> return 4
Michael
.

User: "Gianni Mariani"

Title: Re: Static variable 31 Jan 2008 06:07:46 AM
Stefan Istrate wrote:

I have the following code and I don't understand why is printed 0 0 1
4 4.

#include <stdio.h>

int sum(int max)
{
int i; static int s = 0;
for (i=s; i<max; i++)
s+=i;
return s;
}

int main()
{
int j, n= 5;
for (j=0; j<n; j++)
printf("%3d",sum(j));
return 0;
}

Could anyone help me and explain me why this happens? I saw that the
static variable create this behaviour.

static int s = 0;
.... means - there is only 1 s for the process -and- on the first time
that control passes through this definition, initialize s to zero (or
just initialize it to zero since "=0" is pretty much constant.
You could - in theory - rewrite sum as:
int s = 0;
int sum(int max)
{
int i;
for (i=s; i<max; i++)
s+=i;
return s;
}
if there were no other global named s.
The output 0 0 1 4 should make sense now.
sum(0) = 0 and s = 0;
sum(1) = 0 and s = 0;
sum(2) = 1 and s = 1; (added 1 since 1 is less that 2)
sum(3) = 4 (add 1 and 2 to s to make 4)
sum(4) = 4 (since you start at 4 and 4 is not < 4 no iterations are made)
.... no magic.
.
User: "Stefan Istrate"

Title: Re: Static variable 31 Jan 2008 11:09:20 AM
OK, thank you for your responses. Actually, it was a stupid piece of
code who someone gave me and I couldn't figure out why the static
variable wasn't initialized with 0 every time that function was
called. I understand now. :)
.


User: "Ron Natalie"

Title: Re: Static variable 31 Jan 2008 05:55:53 AM
Stefan Istrate wrote:

I have the following code and I don't understand why is printed 0 0 1
4 4.

Could anyone help me and explain me why this happens? I saw that the
static variable create this behaviour.
Thank you!

Do you know what static means in this case? Their is exactly one
instance of s, that is initialized once the first time sum is called
and then remains what ever you set it.
Just what are you trying to do with the sum function, it seems bizarre.
Since you use i<max in your loop, you do know the loop only gets
executed at most max-1 times?
By the way when asking advice from "why does it do this" it's always
useful to explain what you were EXPECTING versus what you observed.
Your code is horrendous C++, and miserable C.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER