Static effects (modifiers, local variables, global variables)

Source: Internet
Author: User



In C, the literal meaning of static can easily lead us astray, in fact, it has three functions.



(1) First to introduce its first and most important one: hidden.



When we compile multiple files at the same time, all global variables and functions that do not have a static prefix have global visibility. For the understanding of this sentence, I would say for example. We want to compile two source files at the same time, one is A.C and the other is main.c.



Here's what a.c.


char a = ' a '; Global variable
void msg ()
{
printf ("hello\n");
}


Here's what main.c.


int main (void)
{
extern char A; extern variable must be declared before use
printf ("%c", a);
(void) msg ();
return 0;
}


The running result of the program is:



A Hello



You might ask: Why are global variables A and function msg defined in A.C used in MAIN.C? As mentioned earlier, all global variables and functions that do not have a static prefix have global visibility and other source files can be accessed. In this example, a is a global variable, MSG is a function, and neither has a static prefix, so MAIN.C is visible for additional source files.



If static is added, it is hidden from other source files. For example, before the definition of a and MSG, add STATIC,MAIN.C to see them. With this feature, you can define a function with the same name and a variable of the same name in different files without worrying about naming conflicts. Static can be used as a prefix for functions and variables, and for functions, static is limited to hiding, and for variables, static has the following two functions.



(2) The second function of static is to keep the contents of a variable persistent. Variables stored in the static data area are initialized at the start of the program and are the only one initialized. A total of two variables are stored in static storage: Global variables and static variables, except that, compared to global variables, static can control the visible range of variables, in the final analysis, static is used to hide them. Although this usage is not common, I would like to cite an example.


#include <stdio.h>

int fun (void) {
static int count = 10; In fact, this assignment statement has never been executed.
return count--;
}

int count = 1;

int main (void)
{
printf ("Global\t\tlocal static\n");
for (; Count <=; ++count)
printf ("%d\t\t%d\n", Count, Fun ());

return 0;
}


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



(3) The third function of static is initialized to 0 by default. In fact, global variables also have this property, because global variables are also stored in the static data area. In the static data area, all bytes in memory default values are 0x00, sometimes this feature can reduce the workload of the programmer. For example, to initialize a sparse matrix, we can place all the elements in one place 0, and then assign values to elements that are not 0. If it is defined as static, it eliminates the first 0 operation. Another example is to put a character array as a string to use, but also feel each time at the end of the character array to add ' s ' too troublesome. If the string is defined as static, it saves the trouble, because it is already ' the '. Do a little experiment to verify it.


#include <stdio.h>

int A;

int main (void)
{
int i;
static Char str[10];

printf ("Integer:%d; String: (begin)%s (end) ", A, str);

return 0;
}


The running results of the program are as follows



integer:0; String: (Begin) (end)



Finally, the static three effect of a sentence summary. First, the primary function of static is to hide, and second, because static variables are stored in the static storage area, it has a persistence and default value of 0.



The above content is from the blog Park Mr. Write hand, writing is quite clear and easy to understand, archive for review. Original address: http://www.cnblogs.com/dc10101/archive/2007/08/22/865556.html



Here is the 2012 School recruit Pen Question question:



1. What is the difference between a static global variable and a normal global variable?



A static global variable is formed by the description of the global variable (external variable), preceded by static.



Global variables themselves are static storage, and static global variables are, of course, static storage methods. 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 entire 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.



  The static global variable is only initialized once, preventing it from being referenced in other file units;



2. What is the difference between a static local variable and a normal local variable?



Changing a local variable to a static variable changes the way it is stored, which changes its lifetime . Changing a global variable to a static variable changes its scope and limits its scope of use.



The static local variable is initialized only once, the next time based on the last result value;



3. What is the difference between a static function and a normal function?



The static function differs from the normal function scope only in this file. Functions that are used only in the current source file should be described as intrinsic functions (static decorated functions), and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to use these functions should contain the header file.



The static function has only one copy in memory, and the normal function maintains one copy of each call.



Static effects (modifiers, local variables, global variables)


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.