Hello to everyone. I found that a static or global variable will lose its "power" if you assign a value after initialization. Here is an example
Code:
#include <stdio.h>
int foo(int x , int y);
//int c=1;
int main(void)
{
int i , j;
for(i=0 , j=5; i<5; i++ , j++)
printf(" The addition of %d and %d is %d. \n \n" , i , j , foo(i,j));
return 0;
}
int foo(int x , int y)
{
static int c=1;
c=2;
printf(" This is the call of the function : %d \n " , c++);
return x+y ;
}
//
For instance I have had trials on the above code that is why I have "//" on the third line.
The output of c variable is 2 , 2 , 2 , 2 , 2
not 2 , 3 , 4 , 5 , 6
If I reject the line c=2;
the output of c variable will be 1 , 2 , 3 , 4 , 5.
My question is... what is happen? The assignment c=2 after initialization has the power to "destroy" a static nature of a variable as we can see?
THank you in advance.