Role of the Static and Const keywords in C language, staticconst keywords

Source: Internet
Author: User

Role of the Static and Const keywords in C language, staticconst keywords

Local variables of the program exist in (stack), global variables exist in (static zone), and dynamic application data exist in (HEAP.

1. Act on variables:

Use static to declare a local variable ------- a local variable refers to the variable defined within the code block {}, which is only valid within the code block (scope ), the default storage method is automatic variable or dynamic storage. That is, the storage unit is allocated to the variable only when the command is executed to the definition of the variable. When the code block is exceeded, the memory unit (Life Cycle) is released ). When a local variable is declared using static, the storage method (Life Cycle) of the variable is changed to make the variable a static local variable, that is, the memory is allocated to the variable during compilation, the storage unit is not released until the program exits. In this way, the local variable can be used to remember the last data. However, because it is still a local variable, it can only be used inside the code block (the scope remains unchanged ).

Use static declaration of external variables ------- external variables refer to variables defined outside all code blocks {}. By default, these variables are static variables. Memory is allocated during compilation and memory units are released when the program ends. At the same time, it has a wide range of scopes, and the entire file is valid and even other files can reference it. To restrict the scope of some external variables so that they are only valid in this file and cannot be referenced by other files, you can use the static keyword to declare them.

Summary: use static to declare local variables and change them to static storage (static data zone), with the scope unchanged. use static to declare external variables, which are static variables, this will only change the connection mode so that it is only valid within this file, and other files cannot be connected or reference this variable.

2. function:

When using static for function definition, it affects the function connection mode so that the function is only valid within this file and invisible to other files. Such a function is also called a static function. The advantage of using static functions is that you do not have to worry about interference with functions of the same name as other files. It is also a protection mechanism for functions.

If you want other files to reference local functions, use the keyword extern when defining the function, indicating that the function is an external function and can be called by other files. In addition, use extern to declare the external function to be used in the file to reference the external function defined in other files.

 

Const: "read-only )"

1. Define Constants

(1) const

Modify variables. The following two definitions are essentially the same. It means that the value of the TYPE variable modified by const is immutable, readonly.

TYPE const ValueName = value;

Const TYPE ValueName = value;

 

(2) Change const to an external connection to expand to the global level. during compilation, memory is allocated and Initialization is not allowed. It is only used as a declaration. The Compiler considers it defined elsewhere in the program.

Extend const int ValueName = value;

 

2. Use CONST for pointers

(1) the pointer itself is a constant that is unchangeable.

Char * const pContent;

Const (char *) pContent;

(2) The Pointer Points to a constant that is unchangeable.

Const char * pContent;

Char const * pContent;

(3) Both are unchangeable.

Const char * const pContent;

(4) There are also different methods: draw a line along the *: If the const is on the left side of *, the const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant; if const is on the right side of *, const is to modify the pointer itself, that is, the pointer itself is a constant.

3. Use CONST in the function

 

(1) const modifier function parameters

A. the passed parameters cannot be changed in the function (meaningless, because Var itself is a form parameter)

Void function (const int Var );

 

B. The content indicated by the parameter pointer is constant immutable.

Void function (const char * Var );

 

C. The parameter pointer itself is a constant and immutable (also meaningless, because char * Var is also a form parameter)

Void function (char * const Var );

 

D. The parameter is a reference. In order to increase efficiency and prevent modification. When modifying referenced parameters:

Void function (const Class & Var); // The referenced parameter cannot be changed in the function.

Void function (const TYPE & Var); // The referenced parameter is a constant in the function and cannot be changed.

 

The effect of such a const reference transfer is exactly the same as that of the most common function by value.

The only difference between all changes to an object is that a copy of the class object will be created before passing by value, and it will directly pass the address, therefore, this transfer is more effective than passing by value. in addition, only the referenced const can pass a temporary object, because the temporary object is a const attribute and is invisible. It exists in a local domain for a short time and therefore cannot use a pointer, only the referenced const pass can capture this guy.

 

(2) const modifier function return value

The const modifier function does not actually return a lot of values. Its meaning is basically the same as that of the const modifier for common variables and pointers.

A.

Const int fun1 () // is meaningless because the return value of the parameter itself is a value.

 

B.

Const int * fun2 () // call time

Const int * pValue = fun2 (); // we can regard fun2 () as a variable, that is, the pointer content is unchangeable.

 

C.

Int * const fun3 () // call time

Int * const pValue = fun2 (); // we can regard fun2 () as a variable, that is, the pointer itself is immutable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.