Understand the role of static effects of static static static variables

 

static part reproduced in Bowen: the role of static

static are common in embedded C programming, what are summarized its role:

1, hidden

When compiling a plurality of files, not all add prefixes static global variables and functions have global visibility

Example: while compiling main.c and ac

. 1 #include <stdio.h>
 2  char A = ' A ' ;          // global variables 
. 3  void MSG () 
 . 4  { 
 . 5      the printf ( " the Hello \ n- " ); 
 . 6 }
. 1 #include <stdio.h>
 2  
. 3  int main ()
 . 4  {
 . 5      extern  char A;    // hint to the compiler variables may be defined in other modules 
. 6      the printf ( " % C \ n- " , A);
 . 7      ( void ) MSG ();
 . 8      return  0 ;
 . 9 }

Compile

linux@linux:~/code$ gcc a.c main.c -o hello
linux@linux:~/code$ ./hello 
A
Hello

  Why global variables defined in the ac functions in a msg and can be used in the main.c? Because not all of the prefix plus static global variables and functions have global visibility, as main.c other source files can access.

  Added static, it will be hidden from other source files. Msg example, before adding with a static, main.c can not find them.

linux@linux:~/code$ gcc a.c main.c -o hello
/tmp/ccfqGr8L.o: In function `main':
main.c:(.text+0xc): undefined reference to `a'
main.c:(.text+0x24): undefined reference to `msg'
collect2: error: ld returned 1 exit status

  With this static properties of the same name can be defined functions and variables of the same name in different files, without worrying about naming conflicts. Static functions and variables can be used as a prefix, for the function in terms of the role of static in hiding only, and for variables, static as well as the following two functions.

  1) to maintain a persistent variable content: static data stored in the variable region will be completed initialization at the beginning of the program is running, is the only time initialization,  (modified static local variable will only automatically released when the entire end of the program. If the function of the local variables where the next call, it does not require re-statement, and will retain the value on the first call deposit.)

  Example program: 1-5 factorial seeking: code derived from: appreciated static static variables

 6 # include <stdio.h> 
 7 
 8 long factor(int n) 
 9 { 
10      static long int f=1; //static 
11      f*=n; 
12      return f; 
13 } 
14 
15 void main(void) 
16 { 
17       int i; 
18       for(i=1; i<=5; i++) 
19          printf("%ld\n",factor(i));
20 }

operation result:

linux@linux:~/code$ ./a.out
1
2
6
24
120

  由于f为静态变量,能在每次调用后保留其值并在下一次调用时继续使用,所以输出值成为累加的结果。若变量f说明为自动变量(去掉static),当main中多次调用factor时,f均赋初Is 1, so the values ​​are output every f = 1 * n

linux@linux:~/code$ ./a.out
1
2
3
4
5

  2) when the static variables are not initialized, initialized by default to 0

  The third role is the default static initialized to 0. In fact, global variables also have this property, because global variables are stored in the static data area. In the static data area, in memory of all the bytes default values are 0x00, sometimes this feature can reduce the workload of programmers.

   Finally, three static role do one sentence summary. First, the static main function is to hide , and secondly because static variables stored in static memory, so it has the persistence and the default value 0 .

Guess you like

Origin www.cnblogs.com/y4247464/p/12362870.html
Recommended