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