Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between static variable inside and outside of a function?

static int count;

int main()
{

 static int count;    

}

Is there any difference between static variables declared inside and outside any function?

(I mean the scope and visibility of the variable count)

like image 647
munish Avatar asked May 03 '11 11:05

munish


2 Answers

Your first count is only accessible within the module (code in that file). Your second count is only accessible within main.

like image 110
T.J. Crowder Avatar answered Nov 15 '22 15:11

T.J. Crowder


When you declare outside of method it will be available to all static method functions written after its declaration. While declaring static variable in method will be accessible by only that method.

like image 32
Harry Joy Avatar answered Nov 15 '22 17:11

Harry Joy