Read this chapter tonight! Fighting to join the studio!!! = ( ̄ˇ ̄) v C provides five storage model/storage classes for variables, which can be described by storage time, scope, and link for a variable.
one, (σ ' ω ') σ about scope, link, storage period:
- Scope (scope): Divided into ① code block scope (block scope)② function prototype scope (function prototype scope)③ file Scope (file scope): Refers to a variable defined outside all functions ④* function Scope: Applies only to goto statements, and is not currently involved.
- link (linkage): Divided into ① external link (external linkage)② internal link (internal linkage)③ empty link (no Linkage
- Storage Period (storage duration): The time that the variable is kept in memory. Divided into ① static storage period (static storage duration): This class variable exists during program execution. ② Automatic storage period (Automatic storage duration): Allocates temporary memory for the variable, and the memory will be freed for other purposes after the fast operation of its code is completed.
Second, then we can explain the following five kinds of storage class O (* ̄▽ ̄*) ブ:
- Automatic variables : With automatic storage period, code block scope, empty link (in fact, I do not quite understand the meaning of empty links (=. =) ... ), whose storage class specifier: auto
Q:What happens if you use the same variable name inside the code block and outside of the code block? So write a paragraph applet ...
1#include <stdio.h>2 intMain ()3 {4 intI= -;5 {6 intI=Ten;7printf"Inner i=%d\n", i);8 }9printf"outer i=%d\n", i);Ten return 0; One}
Results:
In this case, the inner definition overrides the outer layer definition when the program runs away from the inner block of code, and the external variable restores the function again.
- Register Variable: The register variable is stored in a place that can be accessed and manipulated faster than normal memory (if the register request is allowed on the computer's balance, it can be stored in the CPU register, in addition to the automatic variable type stored in faster memory). It is the same in many ways as an automatic variable (also an automatic storage period, a block scope, an empty link), but it is stored in a register rather than in memory, so its address cannot be obtained. Its storage class specifier: Register (* NOTE: The address operator cannot be used even if it is auto but nominally still register)
- static variable with code block scope: variable with static storage period, file scope, external linkage. Static variable variable-the so-called "static" refers to the location of the variable, the storage-class descriptor: Static
About the use of static (> v <):
1#include <stdio.h>2 voidexe ();3 intMain ()4 {5 inti;6 for(i=0;i<Ten; i++)7 exe (); 8 return 0;9 } Ten One voidexe () A { - intFade=0; - Static intstay=0; theprintf"fade=%d,stay=%d\n", fade++,stay++); - -}
The result is Jiangzi:
╰ ( ̄ω ̄o) is visible, each time the function is called, fade will not remember the last time its own value, so each time from the initialization of the value, and the static type of stay remember the value after each call, and each time from the memory value of the operation, that is, the completion of the code block "static."
< (* ̄ー ̄) ゞps: Static cannot be used for function parameters, as follows:
int hahaha (static int aho);//not allowed Yo!
- static variable with external linkage: A variable that is about to be defined that is placed outside all functions, has a file scope, external link, and a static storage period. This type is called the external storage class (External storage Class) (( ̄▽ ̄ ") legendary" class "? ), the class variable is called an external variable (external variable). Use the keyword: extern to declare it again in a function that uses an external variable.
* ( ̄_ ̄| | |) Actually see here does not understand, about concrete how to use extern ... Try it ...
wrote a small code and n minor changes:
Output the value of the outer variable Yui in the function:
① assigns an initial value when declaring an external variable:
1#include <stdio.h>2 intYui;3 voidexe ();4 intMain ()5 {6 exe (); 7 return 0;8 } 9 Ten voidexe () One { A extern intYui; -Yui=233; -printf"yui=%d\n", Yui); the -}
Successfully output "yui=233".
①* Small changes to a function:
1 void exe () 2 {3 extern int yui=233; 4 printf ("yui=%d\n", Yui); 5 }
And then there's no more ... error message: Variable cannot be extern&&initializer at the same time ...
② in the declaration Yui is assigned at the same time, before the int of the function is not added extern:
1#include <stdio.h>2 intYui=233;3 voidexe ();4 intMain ()5 {6 exe (); 7 return 0;8 } 9 Ten voidexe () One { A intYui; -printf"yui=%d\n", Yui); -}
Results:
If you do not give Yui an initial value, the result is this. The Yui in the function here is an independent local variable, temporarily hide the external variable .
③ directly in the function output Yui:
1 void exe () 2 {3 printf ("yui=%d\n", Yui); 4 }
Output "yui=233", which indicates that the external variables are recognized for the entire file block of code.
④ If an external variable is declared somewhere in the middle of the program, the external variable is not visible to its previous function, and the function is visible after it .
⑤ variable definitions and variable declarations,
1#include <stdio.h>2 intYui=233;//variable definition, there must be3 voidexe ();4 intMain ()5 {6 extern intYui//variable declaration, high readability, does not affect the program, can not. The statement here says "citation statement (referencing declaration)"7 exe (); 8 return 0;9}
Σ ( ̄д ̄;) Four points, and sure enough ... Did not read, forget already accustomed to qaq tomorrow continue ...
"CPP. CHAP12 "Storage class, Link and memory management--hard study of slag residue