Global variables: Global variables are known throughout the program, so they can be used by any code snippet, they maintain their values when executed in the program, they can be created outside any function, and any expression can access them.
Global variables are stored in a fixed store, which is set by the compiler, and global variables become useful when multiple functions in a program use a variable. But unnecessary global variables can cause trouble:
1. Global variables occupy memory throughout the program, and are destroyed only when the program is finished.
2. Using global variables where local variables should be used reduces the use of the function, because the global variable must rely on a definition outside of himself.
3. Using a large number of global variables causes the program to cause errors due to unpredictable side effects. (It is possible to cause an error by changing the value of the global variable)
Cases:
#include <stdio.h>
int count; //global variable
int Main ()
{
Count = //can use Count
func1 ();
return 0 ;
}
Local Variables: A variable declared inside a function is a local variable, more specifically a variable created inside a code block called a local variable (the code block is the contents of a pair of {}), the local variable is created in the code block, and the code block is immediately destroyed.
For example:
void Fun1 (void )
{
int x;
x = 10;
}
void fun2 (void )
{
int x;
x =-99;
}
The two x is a local variable. Because the local variable is destroyed in the code block, we can use the static keyword to modify the variable so that his value is preserved. Local variables that are not declared by static are stored on the stack.
Cases:
#include <stdio.h>
int Count; Global Variables
void func1 (void );
int Main ()
{
Count = ;
func1 ();
return 0 ;
}
void func1 (void )
{
int Count; Local Variables
for (count = 1 ; count < ten; count++)
{
Putchar ( '. ' );
}
}
In this example, count is defined two times, a global variable at a time, a local variable once, and when the name of the global variable and local variable is the same, we use the local variable as the reference object in the area of the local variable declaration, and the second period has no effect on the global variable.
Formal parameters: formal parameters appear in the function, and when the function is to accept the arguments, they must declare the variables that accept the arguments, which are formal parameters that, like local variables, are destroyed when the parameters are out of function.
This article is from the "11132019" blog, please be sure to keep this source http://11142019.blog.51cto.com/11132019/1753004
Global variables, local variables, formal parameters