Chapter III Storage types for variables auto, static, register, and Static keywords

Source: Internet
Author: User

The storage type of a variable determines when the scalar is created, when it is destroyed, and how long his value remains. There are three places where variables can be stored:

Normal memory static

Run-time Stack auto

Hardware Register Register

The default storage type of a variable depends on where it is declared:

static variablestatics: variables declared outside the code block are always stored in static memory, not the memory of the stack, unable to assign other storage types to them, static variables are created before the program runs, and persist throughout the execution of the program. He always retains his original value unless he is given a different value or the program ends.

  

Auto VariableAuto: A variable declared inside a code block stored on the stack, called an automatic variable, is created when the program executes the code block of an automatic variable and automatically destroyed when the program execution flow leaves the code block.

The variables inside the code block, if added with the keyword static, can make his storage type static and persist throughout the execution of the program.

  

#include <stdio.h>void showmsg () {Static count = 0;count++;p rintf ("Count:%d\n", count);} int main () {int index;for (index = 0; index <; index++) {showmsg ();} return 0;}

Count is a static variable that persists, so the program outputs:

Modifying the storage type of a variable does not change the scope of the variable, it can only be accessed within the code block by name.

Register: Prompt is stored in the machine's hardware register instead of in memory, called the Register variable. Register variables are more efficient than memory variables, but the implementation relies on the compiler #_#

Register variables are created and destroyed in the same way as automatic variables, but he needs some extra work, and before a function that uses a register variable returns, the values held before these registers must be restored, ensuring that the caller's register variable is not broken, usually through a stack, before the function starts, The values inside the register variables are persisted to the stack, and when the function returns, the values are then returned to the registers.

Because the registers are saved at different times, the machine generally does not provide the variable address of the register.

Initialization

  Static variables and the initialization of the automatic variable is not the same, the static variable is stored in the program link location is determined , you can assign the initialization value for static variables at this time, if there is no assignment, the default static variable is initialized to 0

An automatic variable is a location that cannot be determined when the program is linked, so if there is no initialization displayed, the value of the automatic variable will be junk data . The advantage is that because it is initialized at run time, you can use any expression as the initialization value. It also reduces memory requirements by allocating storage for variables as needed.

int function (int a) {    int b = a + 3;}

The value of B is determined at run time.

About static keywords

  Keywords with different context static have different meanings.

When static is in the function definition, static is used to modify the link property of the identifier when it is declared outside the code block , modified from external to internal, but the storage type and scope of the identifier are not affected. The function or variable declared in this way can only be accessed in the source file in which it is declared.

Static keywords are used to modify the storage type of a variable, from an automatic variable to a static variable, and the link properties and scope of a variable are unaffected when static is declared as a variable inside a block of code . Variables declared in this manner are created before the program executes and persist throughout the execution.

Chapter III Storage types for variables auto, static, register, and Static keywords

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.