Static variable

S

Stefan Istrate

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!
 
R

Ron Natalie

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

Michael DOUBEZ

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
 
G

Gianni Mariani

Stefan said:
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.
 
S

Stefan Istrate

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. :)
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,516
Latest member
TobiasAxf

Latest Threads

Top