The role of static and const keywords in c language

Source: Internet
Author: User

The local variables of the program exist in (the stack), the global variables exist in the (static zone), and the dynamic requisition data exists in the (heap).

1. Acting on a variable:

Declare a local variable with static-------local variable refers to a variable defined within the code block {}, only valid within the code block (scope), its default storage is automatic variable or dynamic storage, that is, when the instruction executes to the variable definition, the variable is allocated the storage unit. Frees the inner deposit element (lifetime) when jumping out of the code block. When you declare a local variable with static, you change the way the variable is stored (the lifetime), making it a static local variable that allocates memory for the variable at compile time, until the program exits to release the storage unit. This allows the local variable to have a memory function that remembers the last data, but because it is still a local variable, it can only be used inside the code block (scope unchanged).

Declaring an external variable with static-------an external variable that refers to a variable defined outside of all code blocks {}, which defaults to a static variable, allocates memory at compile time, and releases the inner deposit element at the end of the program. At the same time its scope is wide, the entire file is valid, and even other files can refer to it. To limit the scope of certain external variables so that they are valid only in this file and cannot be referenced by other files, you can declare them with the static keyword.

Summary: Declare a local variable with static, make it static storage (static data area), scope is not changed, declare an external variable with static, which is itself a static variable, this will only change its connection mode, so that it is only valid within this file, and other files can not connect or reference the variable.

2. Acting on the function:

When using static for function definitions, it has an effect on how the function is connected, so that the function is valid only within this file and is not visible 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 interfering with the functions of the same name as other files, but also a protection mechanism for the function itself.

If you want other files to reference local functions, use the keyword extern when the function is defined, indicating that the function is an external function that can be called by other files. Also, in files that you want to refer to external functions defined in other files, use the extern declaration to use the external function.

Const function: "Read only (readonly)"

1. Defining constants

(1) const

modifier variables, the following two definitions are essentially the same. The meaning of this is that the const-modified variant of type ' value ' is immutable, ReadOnly.

TYPE const VALUENAME = value;

Const TYPE ValueName = value;

(2) Change the const to an external connection, which is used to enlarge to the global, the memory is allocated at compile time, and can be uninitialized, just as a declaration, the compiler thinks it is defined elsewhere in the program.

Extend const int ValueName = value;

2. The pointer uses the const

(1) The pointer itself is constant immutable

char * const pcontent;

Const (char*) pcontent;

(2) The content pointed to by the pointer is a constant variable

const char *pcontent;

char Const *pcontent;

(3) Both are not variable

const char* Const pcontent;

(4) There are different ways to line up along the * number: If the const is on the left side of *, then the const is used to decorate the variable pointed to by the pointer, that is, the pointer to a constant, if the const is on the right side of *, Const is to decorate the pointer itself, that the pointer itself is a constant.

3. Use const in functions

(1) const modifier function parameters

A. The passed arguments cannot be changed within the function (meaningless because Var itself is a formal parameter)

void function (const int Var);

B. The contents of the parameter pointer are constant

void function (const char* Var);

C. The parameter pointer itself is constant immutable (also meaningless because char* var is also a formal parameter)

void function (char* const VAR);

D. Parameters are references, in order to increase efficiency and prevent modification. When modifying reference parameters:

void function (const class& Var); Reference parameters cannot be changed within a function

void function (const type& Var); The reference parameter is constant within the function is not variable

Such a const reference pass is identical to the most common function passed by value, and he prohibits referencing

All modifications of the object, the only difference is that passing by value creates a copy of the class object first, passes the past, and it passes the address directly, so this pass is more efficient than passing by value. In addition, only the Const pass of the reference can pass a temporary object because the temporary object is a const property and is not visible , he exists in a local domain for a short time, so you can't use pointers, only the const pass of the reference can catch this guy.

(2) const modifier function return value

The const modifier function returns a value that is not practical, and its meaning is basically the same as the const-modified ordinary variable and the pointer.

A.

const int FUN1 ()//This is actually meaningless, because the parameter return itself is an assignment.

B.

const int * FUN2 ()//when tuning

const int *pvalue = FUN2 (); We can think of fun2 () as a variable, that is, the pointer content is immutable.

C.

int* Const FUN3 ()//when tuning

int * Const PVALUE = fun2 (); We can think of fun2 () as a variable, that is, the pointer itself is immutable.

The role of static and const keywords in c language

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.