i have written a code for a fibonacci series returning the sum of the complete series, can the local "static int" variable be returned to the main function where the code is trying to print the sum.
Below is my code
#include<stdio.h>
int fiborecur(int n)
{
static int first=0,second=1,sum=0,total=0;
if( n>0 )
{
sum = first + second;
total = total + sum;
printf(" %d", sum);
first = second;
second = sum;
fiborecur(n-1);
}
else
{
return total+1;
}
}
int main()
{
int n;
printf("Enter the series length you want = ");
scanf("%d", &n);
printf("0 1");
printf("\nSum of the series after return = %d\n", fiborecur(n-2));
return 0;
}
static int, you are returning its value, so yes. but this looks like it will fail on a second invocation, since nothing resets the statics