1. What is the purpose of static? (Please specify at least two types)

Source: Internet
Author: User
Tags define local

1. What is the purpose of static? (Please specify at least two types)

1) In the function body, a variable declared as static remains unchanged when the function is called.

2) in a module (but in the function body), a variable declared as static can be accessed by the function used in the module, but not by other functions outside the module. It is a local global variable.

3) in a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.

The preceding content is C. For C ++, see the following three examples !!!

1) Example:

The first role of static is to keep the variable content persistent. Variables stored in the static data area will be initialized at the beginning of the program, which is also the only initialization. There are two types of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible range of variables. Static is used to hide the variables. Although this is not common, I will give an example.

1 # include <stdio. h> 2 3 int fun (void) 4 {5 static int COUNT = 10; // In fact, this assignment statement has never executed 6 RETURN count --; 7} 8 9 int COUNT = 1; 10 11 int main (void) 12 {13 printf ("Global \ t \ tlocal static \ n"); 14 (; count <= 10; ++ count) 15 printf ("% d \ t % d \ n", Count, fun (); 16 return 0; 17}

 

The running result of the program is:

Global local static

1 10

2 9

3 8

4 7

5 6

6 5

7 4

8 3

9 2

10 1

Reference transferred from: http://wenku.baidu.com/link? Url = GeR-U21HvIYGnuF1xmCEmf-SzR6R-Nfj8x2_RRxXi-NfDPN39XGNWpwGrVTZbyb-_ osre3y89lkfoaikqwegx86umsjlfbzxccdqdkppkou

 

 

2) Example:

Its second and most important one is hiding.
When we compile multiple files at the same time, all global variables and functions without the static prefix are globally visible. I will give an example to illustrate this sentence. We need to compile two source files, A. C and Main. C at the same time. The contents of A.C are as follows:

1 char a = ‘A‘; // global variable2 void msg()3 { 4  printf("Hello\n");5 }

The content of Main. C is as follows:

1 int main(void)2 {3     extern char a; // extern variable must be declared before use print 4     f("%c ", a); 5     (void)msg();6     return 0;7 }

The running result of the program is: a hello
You may ask: why can the global variables A and MSG defined in A. C be used in Main. C? As mentioned above, all global variables and functions without the static prefix have global visibility, and other source files can also be accessed. In this example, A is a global variable, MSG is a function, and there is no static prefix. Therefore, it is visible to other source files main. C.
If static is added, other source files are hidden. For example, if static is added before the definitions of a and MSG, Main. C will not be able to see them. This feature allows you to define functions with the same name and variables in different files without worrying about name conflicts. Static can be used as the prefix of functions and variables. For a function, static can only be used for hiding, but static can also be used for variables.

 

3) Example:

Static is initialized to 0 by default. In fact, global variables also have this attribute, because global variables are also stored in the static data zone. In the static data area, the default values of all bytes in the memory are 0x00. In some cases, this feature can reduce the workload of programmers. For example, to initialize a sparse matrix, we can set all elements to 0 one by one, and assign values to elements other than 0.

If it is defined as static, the first 0 operation is saved. Another example is to use a character array as a string, but it is too troublesome to add & rsquo; \ 0 & rsquo; at the end of the character array each time. If the string is defined as static, it saves the trouble because it is & rsquo; \ 0 & rsquo ;.

Try a small experiment.

1 #include <stdio.h>2 int a; 3 int main(void)4 { 5     int i; 6     static char str[10]; 7     printf("integer: %d; string: (begin)%s(end)", a, str); 8     return 0; 9 } 

The running result of the program is as follows:

Integer: 0; string: (BEGIN) (end)
Finally, we will summarize the three functions of static in one sentence. First, the main function of static is to hide. Secondly, because static variables are stored in the static storage area, they have persistence and default value 0.

 

 

From: http://blog.csdn.net/leo115/article/details/8085414

Interview question: what is the role of static?

A: In C, static mainly defines global static variables, local static variables, and static functions.

1. Define a Global static variable: add the keyword static before the global variable to change the global static variable. Global static variables have the following features:

(1) allocate memory in the global data Zone

(2) If Initialization is not performed, the default value is 0.

(3) The variable is visible from the definition to the end of the file in this file.

2. Define local static variables: add the keyword static before the local static variables, and the local variable becomes a static local variable. Static local variables have the following features:

(1) The variable allocates memory in the global data zone.

(2) If Initialization is not displayed, It is implicitly initialized to 0.

(3) It always resides in the global data zone until the program running ends.

(4) its scope is local scope. When the function or statement block defining it ends, its scope ends.

3. Define static functions: add the static keyword to the return type of the function, and the function is defined as a static function. Static functions have the following features:

(1) static functions can only be used in this source file.

(2) The Inline Function declared in the file scope is static by default.

Note: A static function is only a common global function. It is limited by the static function. It can only be used in the compilation unit where the file is located. It cannot be used in other compilation units.

Two new functions are added in the C ++ language: defining static data members or static function members.

(1) define static data members. Static data members have the following features:

(1) memory allocation: allocated in the global data zone of the program

(2) initialization and definition: space must be allocated when defining static data members. Therefore, space cannot be defined in the class declaration.

(3) static member functions. Static member functions are associated with classes instead of class objects. Static member functions cannot access non-static data members. The reason is simple. Non-static data members belong to specific class instances and are mainly used to operate static data members.

(4) Neither static member function nor static data member has this pointer.

 


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.