The usage and scope of keyword static in C language

Source: Internet
Author: User

First, the static in the process design oriented
Reprint: http://blog.csdn.net/celerylxq/article/details/61604991, static global variable before the global variable, plus the keyword static, the variable is defined as a static global variable. Let's first give an example of a static global variable, as follows://example 1#include <iostream.h>void fn (); static int n; Defining a static global variable void main () void the FN () static global variable has the following characteristics: The variable allocates memory in the global data area, and the uninitialized static global variable is automatically initialized by the program to 0 (the value of the automatic variable is random unless it is explicitly initialized); A static global variable is visible throughout the file that declares it, and is invisible outside of the file, and static variables allocate memory in the global data area, including the static local variables that will be mentioned later. For a complete program, the distribution in memory is as follows:code area global Data area heap stack areaThe dynamic data generated by new in the general program is stored in the heap area, and the automatic variables inside the function are stored in the stack area. Automatic variables generally free up space as the function exits, and static data (even static local variables inside the function) is stored in the global data area. The data in the global data area does not free up space because of the function's exit. The attentive reader may find that the code in Example 1 will be static int n; Define static global variable to int n; Defining a global Variables program works as expected. It is true that defining global variables allows for the sharing of variables in files, but there are also the following benefits of defining static global variables:static global variables cannot be used by other files, variables with the same name can be defined in other files, and no conflicts occur;You can change the sample code above to read as follows://example 2//file1#include <iostream.h>void fn (); static int n; Define the static global variable void main ()//file2#include <iostream.h>extern int n;void fn () compile and run Example 2, and you will find that the above code can be compiled separately, But there was an error at run time. Try to put the static int n; Define static global variable to int n; Define global variables compile and run the program again, carefully understand the difference between global variables and static global variables. 2, static local variable before the local variable, plus the keyword static, the variable is defined as a static local variable. Let's start with an example of a static local variable, as follows://example 3#include <iostream.h>void fn (); void main () void FN () Typically, a variable is defined in the body of the function. The local variable is allocated stack memory whenever the program runs to the statement. However, as the program exits the function body, the system will retract the stack memory, and the local variables are invalidated accordingly. Butsometimes we need to save the value of a variable between two calls. The usual idea is to define a global variable to implement. In this way, the variables are no longer part of the function itself, and are no longer only controlled by the functions, which inconvenience the maintenance of the program. Static local variables can solve this problem. Static local variables are saved in the global data area, not in the stack, and each time the value is persisted to the next call until the next time the new value is assigned. Static local variables have the following characteristics: The variable allocates memory in the global data area;a static local variable is first initialized when the program executes to the declaration of the object, that is, the subsequent function call is no longer initialized, the static local variable is usually initialized at the declaration, and if it is not explicitly initialized, the program is automatically initialized to 0; it always resides in the global data area until the program finishes running. But its scope is local scope, when the function or statement block that defines it ends, its scope ends;3. Static functions are defined as static functions by adding the static keyword before the return type of the function. A static function differs from a normal function in that it can only be seen in the file that declares it and cannot be used by other files. Examples of static functions://example 4#include <iostream.h>static void fn ();//declares static function void main () void FN ()//The benefit of defining static functions for static functions:static functions cannot be used by other files, functions with the same name can be defined in other files, and no conflicts will occur;

The usage and scope of keyword static in C language

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.