※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | 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 to the last minute   

  1. ..clause 1..: Longevity of global variable
  2. ..clause 2..: It is shared by all functions.
  3. ..clause 3..: The local variable becomes independent.

[1] Longevity of global variable

In the foregoing paragraph, the variable declared in the function explained longevity was exhausted in the function.
Thinking about how if it is it outside the function does in the course of nature.

It is possible to actually declare outside the function though all up to now variables have been declared in the function.
The variable declared outside the function might be called a global variable.


[   Global variable   ]
Variable declared outside function.
It keeps surviving until the entire program ends.
It is possible to use it from all functions in the declared source file.
The following program is an example that uses the global variable.
 
#include <stdio.h> 

int count; /* Global variable */

int countfunc(void); 

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

1
2
3
It differs from time in the preceding chapter, and the frequency in which the function was splendidly called this time is counted.
The longevity of the global variable is deeply related to this.
The program is survived to the last minute for the global variable.
Therefore, even when the variable is called many times, the numerical value before is memorized.

By the way, variable count : in the above-mentioned program though doesn't initialize.
Beautifully This is not a coincidence with 1, 2, and 3 though output.
When the program begins, the global variable is automatically initialized by 0.

Because the local variable is made at each call of the function
Though it doesn't automatically initialize because it becomes useless when initializing it in every case
The purpose of the global variable is to only have to initialize it only once first.

It returns to contents.


[2] It is shared by all functions.

The global variable is a variable declared outside the function.
It is possible to use it from any function freely unlike the local variable in the function.
The following program is an example of changing variable count from the main function.

 
#include <stdio.h> 

int count; /* Global variable */

int countfunc(void); 

int main(void) 
{
	countfunc(); 
	count = 10; /* Change here */ 
	Countfunc();
	countfunc(); 
	return 0; 
}
int countfunc(void) 
{
	count++; 
	printf("%d\n",count); 
	return count; 
}
The execution result of this program becomes as follows.

1
11
12
Though the value suddenly becomes 11 in the second display if this result is seen
This is because variable count has been changed in the main function.
The global variable is shared by all functions in this manner.

This : it is to be able to acquire substitution and the value from any function by it.
There is scary of not understanding where the value is changed while it is convenient.
Therefore, use the global variable only for the special data shared by the entire program.

It returns to contents.


[3] The local variable becomes independent.

In the preceding clause, it explained that the global variable was shared by all functions.
If, when there is a local variable of the same name as the global variable in the function
I want to experiment which is given priority.
The following program is an example of having declared variable count in the main function.
 
#include <stdio.h> 

int count; /* Global variable */

int countfunc(void); 

int main(void) 
{
	int count; /* It declares in the same name */. 
	
	countfunc(); 
	count = 10; 
	countfunc(); 
	countfunc(); 
	printf("main : count = %d\n",count); 
	return 0; 
}
int countfunc(void) 
{
	count++; 
	printf("%d\n",count); 
	return count; 
}
The execution result of this program becomes as follows.

1
2
3
main : count = 10
First of all, because this program can be executed
It is understood to be able to declare the global variable and the local variable of the same name.

When you see the result to know which to be given priority
Though the value of variable count has been changed in the main function
The numerical value in the countfunc function has not been changed at all.
The value firmly substituted in the function is displayed at the end of the main function.
In a word, the local variable is given to priority in the global variable and the local variable of the same name.

This is a mechanism to maintain the independence of the function explains in the foregoing paragraph.
When the function is copied and it uses it if the global variable is given priority
It is necessary to examine whether it is a name that the variable in the function is the same as the global variable, and it is troublesome.


Though the global variable explained sharing by all functions here
It is shared in one source file accurately.

For instance, the global variable declared at the head of the main.cpp file :.
Though substitution and the value can be referred from all functions in the main.cpp file
,for instance, another file It is not possible to refer from the function of the data.cpp file.

However, if you teach the name and the type of the variable to another file
It comes to be able to use the global variable from all files.

Because it has not explained the method of using two or more source files yet
There is no sense of incompatibility at the present stage even if it is thought that it is shared by all functions.

It returns to contents.


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