Storage and variable scope

What is the difference between initialization and assignment?

  1. Initialization: When you declare a variable assignment at the same time, when the declaration will draw new memory area, and
  2. Assignment:

Storage methods variables: static storage and dynamic storage.

  1. Static storage: When variables defined, allocated a certain amount of memory units, throughout the program, can only initialize memory unit will remain unchanged once assigned multiple times, static storage variables are not initialized, the initial value is 0.

  2. Dynamic storage: allocating any memory cell during program execution, is released after use, such as in the form of parameters of the function parameter is the dynamic storage, when the declaration of the function unit does not allocate memory to the incoming call when its argument,. to allocate memory, then function is completed, the memory is released. if the dynamic storage variable initialization, the initial value is a random number.


How to declare variables of storage?

  1. Global variables include static global variable and global variables, static storage are therefore not an initial value is not 0, static local variables are static memory, dynamic local variables are only dynamic storage.

  2. Plus static prefix is ​​static storage variables, such asstatic int a = 0;

  3. Without static default prefix is the dynamic storage variables, such as int b = 1;(if it is a global variable is so static storage.)


Memory storage space allocation:

  1. Dynamic storage areas: a memory area storing a dynamic variable, i.e. storage, such as function parameter, no prefix plus static local variables (i.e., dynamic local variable), the function returns the address

  2. Static storage area: the storage area of ​​the variable memory with a static storage mode, such as global variables, static global variable (no dynamic global variables).

  3. Program area: storage program statements.


Scope of variables: local and global.

  1. Global variables: in all external function definition (including the main function), all functions can call the global variables are static storage, the default is not initialized 0:00;.

  2. Local variables: internal function definitions, apply only to the current function when no initialization, if the static storage, or 0, if the dynamic storage, compared with random values.

The difference between static global variables and global variables?

  1. Note that they are static memory, dynamic memory variable does not exist primarily different scopes static global variables apply only to the current file, the initial value 0..;
  2. Extern global variables can be used in other types of files, if there are different variables two identical variable name, while a small value is also the initial functions scope 0.;

The difference between static and local variables (dynamic) local variables?

  1. The same scope, but with different memory release period. After the end of the dynamic local variable function execution, memory is released. In the next call Shiyou initializes initial value of the random number.

  2. And then in a static local variable throughout the program memory is not released, or the last value of the initial value is 0, the next call.;


Why static storage is not initialized, the initial value of zero, compared with a dynamic random number?

Guess you like

Origin www.cnblogs.com/just-save/p/11765474.html