The role of static in C language and what are the benefits of using static functions in C _c language

Source: Internet
Author: User
Tags visibility

To learn more about the role and usage of the static keyword in Java, please click here for more information.

In C, the literal meaning of static is easy to lead us astray, in fact, it has a role of three, respectively:

One is the hidden function, for static-decorated functions and global variables

The second is to maintain the persistence function for the local variable modified by static.

Third, because the static, global, and local variables are modified by default, they are initialized to 0

Here I introduce to you:

(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 without a static prefix have global visibility. To understand this sentence, I 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 is declared before use
 printf ("%c", a);
 (void) msg ();
 return 0;
}

The results of the program's operation are:

A Hello

You might ask: Why is the global variable A and function msg defined in A.C used in MAIN.C? As mentioned earlier, all global variables and functions without 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 another source file.

If static is added, the other source files are hidden. For example, add static,main.c to the definition of a and MSG before you can 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 a function, static is limited to hiding, and for a variable, static has the following two effects.

(2) The second function of static is to keep the variable content persistent. a variable stored in a static data area completes initialization and is only initialized once the program is first run. There are two variables stored in the static store: Global and static variables, but compared to global variables, static can control the scope of the variable, in the final analysis static or to hide. 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 never performs a 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 results of the program's operation are:

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 to initialize the default to 0. In fact, global variables also have this attribute, because global variables are also stored in the static data area. in the static data area, all the byte defaults in memory are 0x00, which can sometimes reduce the programmer's workload. For example, to initialize a sparse matrix, we can place all the elements in one place 0, and then assign a few elements that are not 0. If defined as static, it eliminates the operation of starting with 0. For example, to use a character array as a string, but also feel that each time at the end of the character array to add "" is too cumbersome. If you define a string as static, this will save you the trouble, because there is a ' yes '. You might as well do a little experiment to verify.

#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 result of the program is as follows

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

Finally, the static three function to do a summary of the sentence. The main function of the static first is to hide, and second, because the static variable is stored in the quiescent store, it has a persistence and default value of 0.

The above content is about the static function in C language.

Here are some of the benefits of using static functions in the C language.

The benefits of using static functions in the C language:

Static functions are automatically assigned to a storage area that is always in use until the application instance is exited, avoiding the call function when the stack is pushed out, much faster.
The keyword "static", translated into Chinese is "still", so the internal function is also called static function. But the meaning of "static" here is not the way of storage, but the scope of the function is limited to this file. The advantage of using internal functions is that when different people write different functions, they do not have to worry about the functions they define, whether they will have the same name as the functions in other files, because the same name does not matter.

C language static semantics 1.static variable: 1. Local A. Static local variables are defined within the function, the lifetime is the entire source program, but the scope is the same as the automatic variable and can only be used within the function that defines the variable. After exiting the function, it cannot be used, although it continues to exist. B. For static local variables of the basic type if the initial value is not assigned in the description, the system automatically assigns a value of 0. For an automatic variable, the value is indeterminate. 2. Global global variable itself is the static storage mode, static global variable is of course also static storage mode. But their scope, the scope of a non-static global variable is the entire source program (multiple source files can be used together), while a static global variable restricts its scope, which is only valid within the source file that defines the variable, and cannot be used in other source files of the same source program. 2.static functions (also called internal functions) can only be called by functions in this file, not by functions in other files of the same program. Different from the General non-static function (external function) static in C can be used to modify variables, can also be used to modify the function. First look at the time to modify the variable. Variables in C can be divided into the existence of global data areas, stacks and heaps. In fact, we usually say the stack is stack and does not contain the right, do not confuse.

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.