※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Variable that survives in function   

  1. ..clause 1..: Longevity of static local variable

[1] Longevity of static local variable

Though the variable with different local variable, global variable, and longevity has been taken up
Actually, it is these two intermediate existence, and the variable with a changeable feature exists.

In static (static) and applying before the name of the type when it declares a variable in the function,
A static local variable can be declared.
The following program is an example of having declared a static local variable.

 
#include <stdio.h> 

int countfunc(void);  

int main(void)  
{
	countfunc();  
	countfunc();  
	countfunc();  
	return 0;  
}
int countfunc(void)  
{
	static int count; /* static local variable */ Count++;  
	printf("%d\n",count);  
	return count;
}
The execution result of this program becomes as follows.

1
2
3
One value has increased at each call though it declared in the function.
The value of the start is 0 though not initialized.
This is in every respect seen only the global variable.

However, variable count is a local variable because it is declared in the function.
Actually, if variable count is used in the main function, it becomes an error.

This is a feature of a static local variable.
Being possible to use it : only in the declared function because it is declared in the function.
The value remains until the program ends.
Moreover, it is automatically initialized by 0 even if it doesn't especially initialize it.

Because initialization is done only once in the beginning
For instance, it is possible to count when initializing it as follows.

 static int count = 0; /* Static local variable */
This variable is used to remember the value when the function was called before.
The call frequency of the function is counted though the usage is limited.
When the character position found before is memorized by the retrieved function, it is thought.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.