edited by
673 views
1 votes
1 votes

edited by

1 Answer

2 votes
2 votes
#include <stdio.h>

void f() {
static int x = 5;
x++;
printf("%d", x);
}

void main(){
    int i ;
    for (i=0 ;i< 5 ; i++)
        f();
}

 

This code is different from the code given in question 

How?

static int x = 5; is a definition and will not be executed multiple times hence only first time x will be assigned value 5 but rest of times function f() will just increment x++. 

But the code given in question has also defined x as

static int x; 

x is assigned to value 0 because if not initialized explicitly static variables are initialized with 0 just as global variables.

so this line "static int x; " will not be executed again when function is called but the next line x = 5; will be executed as many times as the function is called so everytime the function f is called x is set back to 5. 

66666 will be printed

 

Related questions

0 votes
0 votes
1 answer
1