The difference between global variables, static global variables, static local variables and local variables

xiaoxiao2021-03-06  104

Variables can be divided into: global variables, static global variables, static local variables, and local variables. Press storage area, global variables, static global variables, and static local variables are stored in the memory-static storage area, and local variables are stored in the memory area. According to the role domain, the global variable is valid throughout the project file; the static global variable is only valid within the file defined; the static local variable is only valid in the defined function, but the program only assigns a memory, the function returns, This variable does not disappear; local variables are valid within the function of defining it, but the function returns to fail. Global variables and static variables are initialized by the compiler to 0 by the compiler if there is no manual initialization. The value of local variables is not known. The code is as follows: //test.cpp#include Extern Int a; // Variable declaration, not defined static int b = 5; void func1 () {cout << b << endl << a << endl ; // The result is 5}

//main.cppint a; int b = 10; void func2 () {static int i; i ; cout << i << endl;}

Void func1 ();

Void main () = 20; func1 (); func2 (); func2 ();} output is 52012

转载请注明原文地址:https://www.9cbs.com/read-95773.html

New Post(0)