Analysis of variables in c language (local variables, external variables, static variables, register variables)

Hits: 0

Variables in C language are divided into four categories:

1.auto automatic variable

  1. [Static]   static storage allocation variables (also divided into internal static and external static)

  2. [extern]  global variable (for external variable description)

4.register   [register] variables (allocated in hardware registers)

Four categories, all variables must be explained (defined) first, and then used.

Introduced below

1. Automatic variables (local variables)

local variables : variables declared at the beginning of a function or at the beginning of a section,

It has several features,

a, the scope is the function that defines it

b. The compiler will not give an implicit initial value to the automatic variable, so its value is uncertain, so the initial value must be explicitly set before each use.

c, formal parameters are automatic variables, the scope is limited to the corresponding function

d. The automatic variable exists and disappears with the reference of the function, and does not maintain the value from one call to the next call.

#include <stdio.h>

void print_number (int x, int y) 
{
         int m,sum;
         for(m = x;m<y;m++) {
                 sum +=m;
        }
        printf("%d\n",sum);
}

void main(void) 
{
     print_number(3,5);
     print_number(3,5);
}

The values ​​printed by this program are not correct, because sum is not assigned an initial value, resulting in the printed number being a random number. We assign the initial value of sum to 0 and then execute the following, and the result is correct. This program shows that local variables must be assigned initial values ​​before being used. In addition, the life cycle of local variables starts when the function is called and dies when the function is called.

2. External variables : Variables defined outside the function are external variables, and their scope is the entire program (global variables).

a. C programs can be placed on several files, and each file can be compiled separately as a compilation unit. An external variable only needs to be defined once on a certain file. When other files want to refer to this variable, use extern to describe it (the extern keyword does not need to be added when an external variable is defined).

b. In the same file, if the preceding function refers to the external (outside the function) variable defined later, add extern to the function to explain.

Reasons for introducing external variables: to solve the coordination of separate compilation of functions; related to variable initialization; the value of external variables is permanent; to solve data sharing;

Note: the function itself is also considered an external variable

3. Static variables: divided into internal [static variables] and external static variables

Internal static variables: a. Add static before local variables to be internal static variables

b. A static local variable is still a local variable, and its scope is still within the scope of the function in which it is defined, but it uses static storage allocation (allocated by the compiler at compile time, while general automatic variables and function parameters use dynamic Storage allocation, that is, allocating space at runtime), when the function is executed and returns to the calling point, the variable will not be cancelled, and its value will continue to exist when it is called again.

External static variable: a. Adding static before the variable defined outside the function is the external static variable

b. The scope is the file in which it is defined, that is, it becomes a private variable of the file. Functions on other files must not be directly accessed unless they are operated through functions on the file where it is located, which can realize data hiding.

4. Register variables: Only automatic (local) variables and function parameters can be further specified as register storage classes

a. Using register variables can improve the access speed, but the number of register variables depends on the specific machine, and only the first few are valid if there are many declarations.

b. Only limited to int, char, short, unsigned and pointer types with registered classes.

c. Cannot take address of register variable (ie & operation)

Initialization of variables:

External variables and static variables are given an implicit initial value of 0 by the compiler;

The initialization of local variables is initialized every time the function is entered.

External or static variables are initialized only once at compile time.

Automatic variables or register variables can only be explicitly initialized, otherwise they will have indeterminate values.

The description of external data, if it has an initializer, is treated as a definition.

In C language, functions cannot be nested, but variables can be nested.

inti = 0;

main( )

{

inti = 1;

printf(“i=%d,”, i);

{

inti = 2;

printf(“i=%d,”, i);

{

i += 1;

printf(“i=%d,”, i);

}

printf(“i=%d,”, i);

}

printf(“i=%d\n”, i);

}

Result: i=1, i=2, i=3, i=3, i=1

You may also like...

Leave a Reply

Your email address will not be published.