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

Source: Internet
Author: User


The variables in C language are divided into four categories, respectively

1.auto Automatic variable

2.static Static Storage allocation variable (also divided into internal static and external static)

3.extern whole variable (for external variable description)

4.register register variable (allocated in hardware registers)

Four categories, all variables must be described first (defined), after use.


The following are described separately

1. Automatic variables (local variables)

local variable : A variable that is described at the beginning of a function or at the beginning of a paragraph, and it has several characteristics,

A, the scope is the function that defines it

b, the compiler does not give the implicit initial value of the automatic variable, so its values are uncertain, so each use must be clear before the initial value.

C, the formal parameter is an automatic variable, the scope is limited to the corresponding function

D, an automatic variable exists and disappears with a reference to the function, and the value is not persisted between the call and 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);
}
This program does not print the correct values because sum does not have an initial value, resulting in a random number of printed numbers. We give sum an initial value of 0 and then execute the following, the result is correct, this program shows that local variables must first be assigned to use the initial value, and also that the life cycle of local variables to start when the function call, the function call when the completion of the extinction.


2. External variable : The variable defined outside the function is an external variable, and its scope is the entire program (the whole variable).


A. C programs can be placed on several separate files, and each file can be compiled separately as a compilation unit. External variables are defined only once on a file, and when other files are to reference this variable, extern is used to explain (external variable definitions do not need to be added with extern keyword).

B. In the same file, if the preceding function refers to an external (outside function) variable that is defined later, it is explained in the function Riga extern.
The reason for introducing external variables: Solving the coordination of separate compilation of functions; related to variable initialization; The value of an external variable is permanent; To resolve 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 variable: A. To add static to a local variable is an internal static variable

B. A static local variable remains a local variable whose scope is still within the range of the function that defines it, but it uses static storage allocations (which are allocated by the compiler at compile time, and the general automatic variables and function parameters are dynamic storage allocation, that is, allocating space at runtime, when the function is done, return to the call point, the variable does not undo, when called again, its value will continue Exist.


External static variable: A. A. Static variable before the function defined externally

B. A scope is a file that defines it, that is, a private variable of that file, and no function on any other file can be accessed directly, except through the functions on the file on which it resides, which enables data hiding.


4. Register variable: only Automatic (local) variables and function parameters can be further specified as register storage class

A. The use of register variables can improve access speed, but the number of register variables depends on the specific machine, the declaration is more than the first few valid.

B. Only limited to Int,char,short, unsigned and pointer types with the Register class.

C. Cannot address the register variable (that is, & operation)





initialization of a variable:

External variables and static variables are given an implied initial value of 0 by the compiler;
Initialization of local variables is initialized once for each entry function.
External or static variables are initialized only once at compile time.
An automatic variable or register variable can only display initialization, otherwise there will be an indeterminate value.
The description of the external data, if it has an initialization item, as a definition.
Functions cannot nest definitions in C language, 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);

}


Results: I=1, i=2, i=3, i=3, I=1

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.