[C language learning] storage type

Source: Internet
Author: User

There are four Storage types in C: auto, static, extern, and register.

★Auto storage type

Default storage type. In C, if the storage type of variables is ignored, the compiler willAutomatic defaultAuto type

★Register storage type

Register variable.The register is on the CPUIt features low capacity and high speed. It is generally used to store frequently used variables in programs, or someIntermediate variableTo speed up program execution. Such variables give priority to registers.

★Static storage type

Static variables are static variables used to restrict the scope. Both global variables and local variables are stored in data segments. This keyword is mainly used in three aspects: local variables, global variables, and functions.

● Apply to local variables

For a local variable whose storage type is declared static, itsSCOPE has not changedOnly the function that defines the local variable can use it. However, itsLifecycleChanged. The declaration period of a common local variable is the execution length of the function that defines it. After the execution of the function that defines it, the local variable will be canceled. But for static local variables, its lifecycle isThe entire function execution period.

● Applications and global variables

For a global variable whose storage type is declared static, itsThe lifecycle remains unchanged., During program execution, the variable always exists, but itsReduces the impact. Because the global variables defined as static can only be used by functions in this file.

Why does the scope of a global variable become smaller when it is defined as static?

Because a project does not only contain a source file, but may contain many source files.Common global variables are visible in other source files.Its scope is the whole project. to reference this global variable in other files, you only need to declare it with extern. However,When declared as static, its scope is limited to the use of this source file. This global variable is hidden and invisible to other files.

In fact, the static keyword has many advantages. In programming, different people complete different modules. To prevent different modules from using duplicate global variables, add a static variable before the global variables of each module. Also, if the external variables in this file do not need to be referenced by other files, you can set them to static external variables to avoid misuse of other files!

● Applications and functions

If static is added before a function, the function scope is only available in this file and invisible to other files.

★Extern storage type

This storage type mainly aimsReference global variables and functions of other files.


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.