C language basics - variables, constants and identifiers

Table of contents

variable:

        definition:

        time angle

        spatial angle

        automatic variable(auto)

        external variables (extern)

        static variable(static)

        Register variable (register)

constant:

Identifier:

Identifier rules:


variable:

        definition:

        Refers to the amount that its value can change when the program is running, and serves the function of storing data.

        In C language, a variable is an abstract definition of the memory space occupied by data in the program. When defining a variable, the user defines the name and type of the variable. These are the operational attributes of the variable. Not only can the variable be accessed through the variable name, variable, the system also uses this identifier to determine the location of the variable in memory

        In computers, there are two types of storage units that store the current values ​​of variables , one is memory, and the other is CPU registers. The storage type of a variable is related to the storage location of the variable. There are four storage attributes defined in the C language, namely automatic variables (auto) , external variables (extern) , static variables (static) and register variables (register) , which are related to the variables. The storage location in memory determines the retention time of the variable and the scope of the variable.

        time angle

        The retention time of a variable is also called the lifetime . From a time perspective, variables can be divided into two situations : static storage and dynamic storage .

        static variable

        Refers to the static storage area where variables are stored in the memory. The storage space is allocated during compilation. During the running of the entire program, the variable occupies a fixed storage unit. This part of the space is released after the program ends. The value of the variable remains throughout the program. always exists in

        dynamic variables

        Refers to the dynamic storage area where variables are stored in the memory. During the running of the program, only when the function where the variable is located is called, the compilation system temporarily allocates a memory unit for the variable. After the function call ends, the variable space is released, and the variable's The value only exists during the function call

        spatial angle

        The scope of a variable is also called a scope . From a spatial perspective, variables can be divided into global variables and local variables.

        global variables

        The scope of variables defined outside the function is from the point of definition to the end of this file. When compiling, the compilation system allocates a fixed memory unit to it, and it occupies a fixed unit from beginning to end when the program is running.

        local variables

        Automatic variable (auto) Automatic variable (auto) Automatic variable (auto) A variable defined within a function or compound statement. It is only valid within the function or compound statement. When compiling, the compilation system does not allocate memory units for local variables, but During the running of the program, when the function where the local variable is located is called, the compilation system temporarily allocates memory as needed, the call ends, and the space is released.

        automatic variable(auto)

        Local variables in a function, unless specifically declared as a static storage class, are dynamically allocated storage space, and the data is stored in the dynamic storage area. The formal parameters in the function and the variables defined in the function (including variables defined in the compound statement) belong to this category. The system will allocate storage space for them when the function is called, and the storage will be automatically released when the function call ends. space, such local variables are called automatic variables

                                                        Use auto to declare

        external variables (extern)

        That is , global variables are defined outside the function. Its scope starts from the variable definition to the end of the program file.

        If an external variable is not defined at the beginning of the file, its valid scope is limited to the point of definition to the end of the file. If the function before the definition point wants to reference the external variable, the variable should be "external variable declared" using the keyword extern before the reference , indicating that the variable is an already defined external variable. With this declaration, you can legally use the external variable from the "declaration" point

        Usually, use extern to declare external variables and expand the scope in the program file.

        static variable(static)

        The value of local variables in a function does not disappear after the function call ends but retains its original value.

        By using a variable declared with a static type, the memory space of the variable is located in the global static area of ​​​​the memory and will only be initialized once.

 

        The reason for this result is that each time the value of a continues to be calculated from the value of the previous function call, it is not initialized and calculated from 5 every time. This is the characteristic of static

        static is not used:

        Static is not used, and calculation starts from a = 5 every time

        Register variable (register)

        In order to improve efficiency, C language allows the value of local variables to be stored in CPU registers. Such variables are called register variables and are declared with the keyword register

        When using register variables, you need to pay attention to the following points:

        (1) Only local automatic variables and formal parameters can be used as register variables

        (2) The number of registers in a computer system is limited, and any number of register variables cannot be defined.

        (3) You cannot use the address operator "&" to find the address of a register variable.

        In today's iterative development of C language, when we try to use register to declare a variable, the compiler is likely to ignore "register" according to the situation. Everyone needs to be aware of this situation.

constant:

Refers to a quantity whose value does not change when the program is running. Constants do not occupy memory. When the program is running, they appear directly in various registers of the arithmetic unit as operation objects.

#include <stdio.h>
int mian()
{
        int  year;
        year  =  2023;
        printf("welcome to C's word!\n");
        return 0;
}

Variable: year Constant: 2023

Identifier:

        The variable names, function names, labels, etc. used in the program are collectively called identifiers. Identifiers can only be strings composed of letters (A~Z, a~z), numbers (0~9), and underscores (_).

        Identifier rules:

        (1) Standard C does not limit the length of identifiers, but it is limited by various versions of the C language compilation system and is also limited by specific machines. For example, a certain version C stipulates that the first eight digits of an identifier are valid. When the first eight digits of two identifiers are the same, they are considered to be the same identifier.

        (2) In identifiers, there is a difference between upper and lower case. For example, CLANG and Clang are two different identifiers.

        (3) Although identifiers can be defined arbitrarily by programmers, identifiers are symbols used to identify a certain quantity. Therefore, the naming should have corresponding meaning as much as possible to facilitate reading and understanding, so as to achieve "just as the name implies".

Guess you like

Origin blog.csdn.net/m0_74436212/article/details/131230377