A thorough understanding of C Variables

Source: Internet
Author: User

A thorough understanding of C VariablesVariables are the most basic and core concepts of C language. C variables are complex. Here we will make a summary and hope to help students who want to learn C.I. Recognition VariablesThe literal meaning of a variable refers to a variable data. There are various types of data. The variable type specifies the data types that can be represented by the variable. Understanding the nature of variables from the storage perspective, for example: char c = 'X'; this statement defines a char variable c, the value of the variable is the ASCII code of the letter 120x120). The variable is stored in a memory area of the computer, and the address number of the storage unit is assumed to be 0x1101. Variable name c is essentially a symbol of memory unit address 0x1101. c Represents the memory address 0x1101. When variable c is used, it is essentially the memory unit for access. The variable can be changed because the data in the memory can be changed, so it is called a variable. If you do not understand the computer content, see computer composition principles. In fact, the principle of computer composition is to learn the compilation and C language first courses, but the university is often upside down, misleading a batch of other high-tech students.2. Recognition of variable classification from the perspective of Time and SpaceWhen I first learned the C language, I often had a lot of questions, such as register variables and global variables. In particular, these concepts crossed the external register variables, so that beginners would be dizzy. In fact, variables have their own time-space characteristics. In short, it can be divided into global variables and local variables from the scope space perspective. From the perspective of storage type lifecycle/time, it can be divided into static storage and dynamic storage. In addition, there are many types in the expression value type division of variables. Generally, int type variables mean this.Iii. Understanding the Storage types of VariablesTo understand the storage type, you need to know the division of the memory areas of the C language program, which is generally divided into the following five parts: 1. Stack zone Stack): Stack is the memory area automatically allocated and released by the compiler, used to store local variables, including method parameters ). The number of variables and the value of the variables in the stack area change with the running of the program. 2. Heap): the memory area applied by the programmer through the C function such as malloc), which is released after use or automatically released by the OS ). 3. Static Global Static zone): This memory zone is used to save global and Static variables. Allocated when you enter the program, the program is released by the system after it is completed, and the program continues to exist throughout the program running. 4. Text constant area: the memory area used to save the constant string. After the program ends, it is released by the system. 5. program code area: the memory area used to save the program binary code.IV. C variable storage type keywordsThe C variable storage type description keywords include: 1. auto variables belong to local variables and are dynamically stored. Auto can be omitted. The system does not initialize it by default. uninitialized static variables point to an unexpected and meaningless value. 2. The register variables, like auto, belong to local variables and are dynamically stored. However, the storage is in the CPU. The get address operator (&) cannot be used for variables because there is no memory address in the storage of register variables. 3. extern external variables are defined outside the function, also known as global variables. They are stored in the Global static zone using static storage ). External variables and global variables are two different methods for variables of the same type. The full set of variables is proposed from the perspective of his scope, and the external variables are proposed from his storage method, indicating his life cycle. 4. static variables are stored in the static zone and can be classified into static global or external) and static local or internal. For static local variables of the basic type, the system automatically assigns the initial value 0 the same as the external variable ). But static variables are not necessarily static variables, and external variables are global variables. Note: For register variables, many C compilers do not actually put register variables inside the CPU, but use them as automatic variables. Using register only prompts the compiler to define a variable as a register variable, and expects the compiler to use it as a register variable, but it may not be able to meet expectations. Because it depends on whether the compiler supports it, and then check whether there is available register space in the CPU.

This article is from the "melyan" blog, please be sure to keep this source http://lavasoft.blog.51cto.com/62575/186951

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.