C language-variable description

Source: Internet
Author: User

Standard Format of variable Declaration

Storage type data type variable name. For example, static int I = 5; (each variable includes at least the storage type and data type .)

Note: before learning the details, you 'd better first look at the keywords, whether it is learning IT technology, learning language and other knowledge. The learning process is the process of deep understanding of keywords. If you have a deep understanding of the following keywords, you do not need to look at the following. (It's not enough to change to a keyword in school. If the keyword is learned, it is qualified .)

1: int, double, float, Char, void, Char.

2: auto, extern, static, and register.

I. Three main features of variables:

Variable data type: It mainly indicates the memory space occupied by the variable, such as the int type.

Scope of a variable: The valid range of the variable, that is, the scope of use of the variable.

Storage Type of variables: Storage Method of variables in memory. Different Storage Methods affect the life cycle of variables in the memory.

2. Data Types of variables:

The Data Types of variables include int, float, double, and Char. For details, see other materials.

Iii. Scope of variables:

1. Local variables:

1.1 Definition: a variable defined within a function or a composite statement is called a local variable.

1.2: feature: the scope of use is to define its functions or compound statements.

1.2.1: local variables are placed in the dynamic storage area. During compilation, the system does not allocate storage units for them. The system allocates memory units only when the program runs to the function where the local variables are located. When the call ends, the memory unit is released. That is, the lifetime of the variable is the time when the function is called.

1.2.2: local variables can have the same name in different functions. They will not conflict as long as they have different scopes. If the variable name is the same, the current local variable takes precedence.

2. Global variables:
2.1 Definition: variables defined outside all functions (and, of course, outside the main function) are called global variables.

2.2. features: the scope of a global variable ranges from the position of the defined variable to the end of the source program, and the default value is 0.

2.2. global variables are stored in the static storage area, and memory units are allocated to global variables during compilation until the program stops running. The lifetime of the global variable is the entire program running period.

2.3 if a global variable with the same name as a local variable takes precedence over a defined function.

2.4 if you want to use a local variable before the definition, use extern to declare the scope of the global variable that can be expanded.

Iv. Storage of variables:

1. There are two storage methods for variables: static storage and dynamic storage.

2. The storage mode of variables is jointly determined by the scope of variables and the storage type of variables, and can be determined only from the scope.

There are four types of C storage:

1. Auto: Automatic variables (default compiler type, system default for short)

2. Register: register variable

3. Static: static type

4. extern: External type

Auto Type Details:

If the storage type is not added, the default value is auto.

1. Example: int A = 4; <=> auto int A = 4; (<=> equivalent symbol ).

2. The parameter variable is an automatic variable.

3. Automatic variables are local variables.

Register Type Details:

The register type is different from the general type: the Register type variables are stored in the CPU registers, and the operation speed is faster than the variable stored in the memory. Generally, variables used repeatedly are put into registers to speed up program running.

Static type variable

1. Static variables are stored in static mode. Static storage is not necessarily a static variable. Such as external variables.

2. Static variables include static local variables and static global variables.

1: static local variables: add the storage type specifier before the local variables

Static is a static local variable.

1.1: differences between static local variables and local variables:

1.1.1 static local storage is a static storage method, and local variables are a dynamic storage method.

1.1.2: The definition field is the same. Only when the function is defined, the value of the automatically typed variable disappears after the function call ends, and the value of the static local variable remains in the memory, it cannot be called externally. When this function is called again, the value of the static variable continues to be valid and is equal to the value at the end of the last call.

For example:

Void teststatic ()

{

Static int I = 1;

Printf ("% d \ n", I ++ );

}

Int main ()

{

Teststatic (); // The result is to print the number 1.

Teststatic (); // The second call result is printed with 2 instead of 1.

Return 0;

}

1.1.3 difference in survival: the lifetime of static local variables is the entire program running, and the lifetime of dynamic local variables is the end of function call.

1.1.4. The system automatically assigns an initial value of 0 to the static local variable, while the initial value of the automatic variable is uncertain.

Static global variables

2: static global variables:

Adding the storage type description before the global variables static constitutes a static global variable.

2.1. Differences between static global variables and global variables: the scope of global variables is the entire program. The scope of a static variable is the source file that defines the variable.

Conclusion: The effect of static on local variables changes the survival time of local variables. The scope global variable changes the scope of use of the global variable.

Extern type variables:

1. extern type definition:
External types are global variables that are defined outside all functions and are permanently allocated to the system during compilation.

Ii. features:

1. As with static variables, the system automatically assigns 0 values to uninitialized variables.

2. The external variables are the same as the global variables. The scope is from the definition to the end of the entire source program.

3. external variables can be used by other files. For example, only static delimiters can be used in this file.

For example, the file a. C and file B. C define the external variable int cctwl = 520 in B. C;

The contents of the. c file are as follows:

Main ()

{

Extern int cctwl; // declare the external variable. The Compiler automatically finds the variable cctwl in B. C.

Printf ("% d", cctwl); // result 520.

}

Before calling an external variable, you must use the extern statement. However, the declaration of an external function can omit the extern and directly write the function prototype. Extern is added by default.

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.