Summary of variable Storage

Source: Internet
Author: User

Variable is the basic and core concept of C language. For beginners of C language, variable storage is always confused. Here is a simple summary of the C variable, hoping to help you.

We can see many concepts, attributes, storage period, internal links, external links, empty links, global variables, local variables, register variables, static variables, and so on. It looks messy. Let's sort it out.

In fact, when we use variables again, we focus mainly on three aspects: the existence period of variables, the type of variables, and the scope of variables.

The variable definition method is divided into three parts:

<Storage Class> <variable data type> <variable name>

Understanding

Variable name. This is very simple. We only need to comply with the naming rules of variables. It may consist of numbers, letters, and underscores (_). It must start with a letter or underscore and cannot be a keyword.

Variable type. This is not hard to understand. We know there are INTEGER (INT, long), character (char), floating point (float, double), pointer (*), and of course array struct. I believe that we are no longer familiar with this.

Storage Class. This is the focus of our discussion today. The storage category of Data determines the existence period and scope of a data. First, let's take a look at the concepts of existence period and scope.

Retention period: the time when the variable is stored in the memory. In fact, there are two main scenarios. One is that the function works with the function call and ends with the function end. The second is to act on the entire program period until the program ends.

Scope: the region where the variable can work. In simple terms, there are three situations: one is to act only in a code block (or in a function), and the other is to act on the entire file (or program, third, you can use other files except the file.

So how does our storage category determine the period of existence and scope for a long time. Don't worry, it's just a combination of the following keywords and the location where the variables are defined.

Auto is the Storage Class Identifier. It can be said that a variable is an automatic variable. If the storage number type is omitted, auto is used by default. The modified global variables are in the static zone, and the local variables are in the stack.

The variables declared by register belong to the Register storage class. All modified variables are saved in registers.

Static declared variables belong to the static storage class. All the modified variables are in the static zone.

Use the extern keyword in the external variable function to declare it again.

We can sum up the combinations of these keywords and locations to make it clearer:

Combination Storage Period Scope Initial Value
Auto + is defined in the function. Synchronize with functions Function Uncertain
Auto + is defined outside the function. Synchronize with programs This file (Program) and external files 0
Static + defined in the function Synchronize with programs Function 0
Static + is defined outside the function. Synchronize with programs In this document (Program) 0
Register + is defined in the function Synchronize with functions Function Uncertain

Note that register can only modify local variables and parameters, because there is no address in the Register, and it cannot be static.

Extern: external reference. This parameter is not described too much when an external reference is required.

Here we will only discuss data storage. We will not describe too much about some other features of keywords. I don't know if reading the above table will help you understand the data storage period and scope.


This article from the "embedded learning world" blog, please be sure to keep this source http://farsight.blog.51cto.com/1821374/1559750

Summary of variable Storage

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.