-9

Can someone explain why is the total = 7 in the following code? with static int sum?

    #include <stdio.h>
    int i, j; 
    int madness(int x);
    int main(void) 
    {
    int i, total = 0;
    j = 1;
    for (i = 0; i<3; i++) { total += madness(i); }
    printf("Total = %d\n", total);
    return 0;
    }  

    int madness(int x) 
    {
     static int i;
     static int sum = 0;
     for (i = 0; i<x; i++, j++) { sum += j; }
     return sum;
     }

2 Answers2

1

I added some debug statements to your code to help understand it better.

 #include <stdio.h>
 int i, j;
 int madness(int x);
 int main(void) 
    {
        int i, total = 0;
        j = 1;
        for (i = 0; i<3; i++) 
        { 
            total += madness(i); 

        printf("Total = %d\n\n", total);

    }
    return 0;
}  

int madness(int x) 
{
     static int i;
     static int sum = 0;

     for (i = 0; i<x; i++, j++) 
     { 
         printf("j is %d\n", j);
         sum += j; 
         printf("Sum is %d\n", sum);

     }
     return sum;
}

Also,open the link suggested by @Yashas to understand what static means. Basically, a local variable inside a function is destroyed after the function returns; but a static variable is not destroyed and every time the function is called, the sum will have the same value that it had the last time.

KulaDamian
  • 154
  • 2
  • 14
1
    #include <stdio.h>
    int i, j; 
    int madness(int x);
    int main(void) 
    {
    int i, total = 0;
    j = 1;
    for (i = 0; i<3; i++) { total += madness(i); }
    printf("Total = %d\n", total);
    return 0;
    }  

    int madness(int x) 
    {
     static int i;
     static int sum = 0;
     for (i = 0; i<x; i++, j++) { sum += j; }
     return sum;
     }

1st call to madness function with i=0;

since static variables initialize only once ,so with first call to madness function it assigns i=0 and sum=0.since x=0 in this function so value of sum remains the same and it returns 0.

2nd call to madness function with i=1;

Here we have x=1,j=1,sum=0 and function run loop

 for (i = 0; i<1; i++, j++) { sum += j; }

so we get sum=1 and the value of j got 2 Now which will be used in the next function call.Now j becomes 2 and function return 1. so it's added to our total .Now total becomes 1.

3rd call to madness function with i=2;

here x=2,j=2,sum=1(from previous call static values remains the same) Now you are smart enough to calculate this result

for (i = 0; i<2; i++, j++) { sum += j; }

here sum becomes 6 and return this value . since our previous value of total is 1 .Now it becomes 6+1=7 Which is your required answer.

Nishant sharma
  • 116
  • 1
  • 11
  • Thanks. does that mean "total" retains value of 1 for 3rd call even its not assigned static – paramount123 Dec 28 '17 at 14:29
  • yeah,'total' is not a static variable.when you call a function 'madness' then it's like you saved the current instance of main function and give control to 'madness' function and when the function ends then again main function gets the control back. got it? – Nishant sharma Dec 28 '17 at 15:06