C-Language storage classes

Source: Internet
Author: User
Tags modifier

Auto ordinary local stack variable: is automatic storage, the limit variable can only be used inside the function, this object is automatically created and destroyed, it is recommended that the variable is placed on the stack, the function is called to allocate memory, the function at the end of the free memory. General Hide auto defaults to the automatic storage category. Most of our program variables are automatic variables.

Example auto.c

1#include <stdio.h>2 3 intMainvoid)4 {5Autointi =9;/*the keyword that declares a local variable is auto; because it can be omitted, almost no one uses*/6printf"%d\n", i);7GetChar ();/*reads a character from the buffer, which is equivalent to clearing the buffer .*/8     return 0;9}

Register Register variable : Dynamic and static variables are stored in memory, the program encountered this value when the controller sent instructions to send the value of the variable to the operator, need to save the number and then saved in memory. If a variable is used frequently, such as multiple loops in a function body, each time the local variable is referenced, we can place the value of the local variable in the register of the CPU, called the Register variable. Does not require multiple accesses to in-memory to improve efficiency. However, only local automatic variables and formal parameters can be used as register variables. Some registers are used when the function is called and released at the end of the function. Different systems do not have the same requirements for register, for example, to define the number of register variables, data types and other restrictions, some default is automatic variable processing. So in the program is generally not used. The register variable has no address.

Example register.c

1#include <stdio.h>2#include <time.h>3 4 #defineTime 10000000005 intM, n = time;/*Global Variables*/6 7 intMainvoid)8 {   9 time_t start, stop;TenRegisterintA, B = time;/*Register Variable*/ One     intX, y = time;/*General Variables*/ A  -Time (&start); -      for(A =0; a < b; a++); theTime (&stop); -printf"Register variable spents:%ld seconds \ n", stop-start); -      -Time (&start); +      for(x =0; x < y; X + +); -Time (&stop); +printf"General variable spents:%ld seconds \ n", stop-start); A      atTime (&start); -      for(M =0; M < n; m++); -Time (&stop); -printf"global variable spents:%ld seconds \ n", stop-start); -  -     return 0; in}

Static static variable (smtie): The default storage class for global variables, which indicates that variables are visible during the program life cycle, and in C, static can be used to modify local variables, global variables, and functions. In different situations, the effects of static vary.

(1) Modifying local variables

In general, the local variable is stored on the stack, and the life cycle of the local variable ends at the end of the statement block execution. However, if modified with static, the variable is stored in the static data area, and its life cycle continues until the end of the execution of the program. However, it is important to note that although the modification of a local variable with static, its life cycle and storage space have changed, but its scope has not changed, it is still a local variable, scope is limited to the statement block.

After modifying a local variable with static, the variable is initialized only at the first run and only once.

1#include <stdio.h>2 voidFun ()3 {4     Static intA =1;5a++;6printf"%d\n", a);7 }8 intMainvoid)9 {Ten Fun (); One Fun (); A     return 0; -}

Program execution results are: 2 3

The second time the fun () function is called, the value of a is 2, and no initialization assignment is made, and the self-increment operation is performed directly, so the result is 3.

For a static local variable, if it is not initialized, the system will automatically assign a value of 0 to the shaping variable, and for the character array, it will automatically be assigned a value of ' \ s '.

(2) Modifying global variables

For a global variable, it can be accessed either in the source file or in other sources in the same project (simply declare with extern).

Example file1.c

1 int a=1;

Example file2.c

1 #include <stdio.h>  2externint  A;   3 int Main (void)  4{  5     printf ("  %d\ ", a);   6     return 0 ;   7

The execution result is 1

However, if the int a=1 is changed to the static int a=1 in file1.c;

Then the file2.c is inaccessible to the variable A. The reason is that the modification of the global variable with static changes the scope of its scope, from the original project visible to the source file is visible.

(3) Modifier function

With the static modifier, the same thing as modifying a global variable is changing the scope of the function.

Extern represents a global variable, which is visible to all files within a program, similar to the public keyword in Java;

From the scope view:

1. Global variables have global scope. A global variable can be used for all source files simply by defining it in one source file. Of course, other source files that do not contain global variable definitions need to declare the global variable again with the extern keyword.

2, the static local variable has a local scope, it is initialized only once, since the first time it was initialized until the end of the program is always there, and the difference between the global variable is that the global variable is visible to all functions, and static local variables only to define their own function body is always visible.

3, local variables are also only local scope, it is an automatic object (auto), it does not persist during the program run, but only during the execution of the function, the function of the execution of a call after the end of the variable is revoked, the memory occupied by the recovery.

4, the static global variable also has the global scope, its difference with the global variable is that if the program contains more than one file, it acts on the file that defines it, it does not work in other files, that is, the variable modified by the static keyword has a file scope. This way, even if two different source files define static global variables of the same name, they are also different variables.

From allocating memory space, see:

1, global variables, static local variables, static global variables are allocated space in the static storage, and local variables allocated space in the stack

2, the global variable itself is the static storage, static global variables, of course, is also a static storage mode. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.

    • 1) Static variables are placed in the program's static data store (globally visible) so that the original assignment can be maintained at the next call. This is the difference between a stack variable and a heap variable.
    • 2) variables are used to tell the compiler that they are only visible within the scope of the variable. This is the difference between it and the global variable.

From the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use. So the function of the static descriptor is different in different places. Should be taken into consideration.

Tips:

    • If the global variable is accessed only in a single C file, the variable can be modified to a static global variable to reduce the coupling between modules;
    • If the global variable is accessed only by a single function, the variable can be changed to the static local variable of the function to reduce the coupling degree between the modules;
    • When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider the reentrant problem, because they are all placed in a static data store and are globally visible;
    • If we need a reentrant function, then we must avoid using the static variable in the function (a function called "Internal memory" function)
    • The static variable condition must be used in the function: for example, when the return value of a function is a pointer type, the address of a static local variable must be the return value, and if it is auto type, it is returned as the wrong pointer.

C-Language storage classes

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.