Static keyword in C
Using a static declaration to qualify external variables and functions limits the scope of subsequently declared objects to the rest of the compiled source file. To specify an object as static storage, you can prefix the normal declaration with the keyword static. There can be at most one storage class specifier in a declaration. If no storage class specifier is specified, the following rules apply: objects declared inside functions are considered to be of type auto; Functions declared inside functions are considered extern types; Objects and functions declared outside functions are considered static and have external connections.
An object or function has an internal connection if its first external declaration contains the static specifier, otherwise it has an external connection.
1. Modify variables
The staITc specifier describes the declared object as a static storage class. This object can be used inside or outside a function. Inside the function, this specifier causes an allocation of storage and has a defining effect. Objects of type static are initialized only once before the program begins execution. An external declaration of an object is a definition if it has an initial value. An external object declaration is a temporary definition if it has no initial value and does not contain an extern specifier. If the definition of an object appears in a translation unit, all temporary definitions are simply considered redundant declarations; If no definition of the object exists in the translation unit, the temporary definition is converted to a definition with an initial value of 0.
In C, both global and static variables are stored in static storage. Global (or static) variables that are not initialized are initialized to 0 by default.
C99 standard, 6.7.8/10 talks about variable initial values (uninitialized statically stored objects are cleared)
If an object that has automatic storage duration is not initialized explicitly, its value isindeterminate. If an object that has static storage duration is not initialized explicitly,then: – If it has pointer type, it is initialized to a null pointer; — if it has arithmetic type, it is initialized to (positive or unsigned) zero; — If it is an aggregate, every member is initialized (recursively) according to these rules; — If it is a union, the first named member is initialized (shrink) according to theserules.
1.1 Global Variables
You can hide external objects by qualifying them static. Features: a. Allocate memory in the global area; B. The variable is visible from the definition to the end of the file;
1.2 Local variables
Static can also be used to declare internal variables. The static type of the internal variables through automatic variables, is a specific function of the local variable, can only be used in the function, but it is different from automatic variables, regardless of whether the function is called, it has always been, and not like automatic variables, as the function is called and exit and exist and disappear. In other words, an internal variable of the static type is a variable that can only be used in a particular function but always occupies storage. Features: a. This variable allocates memory in the global data area; B. It always resides in the global data area until the end of the program; C. Its scope is local. Its scope ends when the function or statement block that defines it ends;
Demo:
#include<stdio.h>
static int nGlobal;
void func1(void)
{
static int nLocal = 0;
nLocal++;
printf("nLocal:%d\r\n",nLocal);
}
void func2(void)
{
nGlobal = 0;
nGlobal++;
printf("nGlobal:%d\r\n", nGlobal);
}
void func3(void)
{
int nTmp = 0;
nTmp++;
printf("nTmp:%d\r\n",nTmp);
}
int main(a)
{
int i = 0;
for(i = 0; i < 10; i++)
{
func1();
func2();
func3();
}
return 0;
}
Copy the code
Print:
nLocal:1 nGlobal:1 nTmp:1 nLocal:2 nGlobal:1 nTmp:1 nLocal:3 nGlobal:1 nTmp:1 nLocal:4 nGlobal:1 nTmp:1 nLocal:5 nGlobal:1 nTmp:1 nLocal:6 nGlobal:1 nTmp:1 nLocal:7 nGlobal:1 nTmp:1 nLocal:8 nGlobal:1 nTmp:1 nLocal:9 nGlobal:1 nTmp:1 nLocal:10 nGlobal:1 nTmp:1Copy the code
2. Modifier functions
External static declarations are usually used for variables, but they can also be used to declare functions. Normally, function names are globally accessible and visible to all parts of the program. However, if a function is declared static, the function name is not accessible except for the file in which the function is declared. “Static” limits the scope of functions to this file (hence, internal functions). The advantage of using internal functions is that when different people write different functions, they don’t have to worry about defining a function with the same name as a function in another file. Features: a. Static functions can only be used in the source file; B. Inline functions declared in the file scope default to static;
Demo1: normal function call. Test () is used as an example. Test.c
#include <stdio.h>
void Test(a);
void Test(a)
{
printf("This is test()\n");
}
Copy the code
main.c
#include <stdio.h>
extern Test(a);
int main(a)
{
Test();
return 0;
}
Copy the code
Print:
Demo2: test() with the static modifier test.c
#include <stdio.h>
static void Test(a);
static void Test(a)
{
printf("This is test()\n");
}
Copy the code
main.c
#include <stdio.h>
extern Test(a);
int main(a)
{
Test();
return 0;
}
Copy the code
Error compiling:
1>------ Generated: Project: ConsoleApplication1, configuration: Debug Win32 ------ 1>test.c 1>main.obj: error LNK2019: Unparsed external symbol _Test, This symbol is referenced in the function _main 1>C:\Users\mints\Documents\Visual Studio 2017\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1120: 1 > 1 cannot resolve the external command generating project has been completed "ConsoleApplication1. Vcxproj" operations - failure. = = = = = = = = = = : zero success, failure, the latest zero, skip the 0 = = = = = = = = = =Copy the code
Refining summary
Make a quick summary of what static does. First, the primary function of static is to hide, and second, because static variables are stored in a static store, they have a default value of 0 (for uninitialized global and local static variables) and persistence.
Knowledge expansion:
Storage class
There are two types of storage classes: automatic and static. Some of the keywords used to declare an object and the context of the declaration together determine the object’s storage class.
Automatic storage classes are local to a block and disappear when the block exits. If the storage class specifier is not used, or if the auto qualifier is used, declarations in the block generate automatic storage class objects. Objects declared as Register are also automatic storage class objects and will be stored in the machine’s fast registers (if possible).
Static objects can be local to a block or external to all blocks. In either case, the value remains the same when exiting and re-entering a function or block. Static objects are declared with the keyword static within a block (including the block that provides function code). Objects declared outside all blocks and at the same level as function definitions are always static. The static keyword can be used to declare objects as local objects of a particular translation unit, and objects of this type will have internal joins. When an explicit storage class is omitted or declared with the extern keyword, objects are globally accessible to the entire program and have external connections.
BBS section
BSS (Block Started by Symbol) is an area of memory used to hold global and static variables that are not initialized or initialized to 0 in a program. Features are: read-write, before the program is executed BSS segment will automatically clear 0. So, uninitialized global or static variables are zero before the program executes.
Chapter and verse
The C Programming Language Second Edtion www.cnblogs.com/ustc-anmin/… C.biancheng.net/cpp/html/43… www.cnblogs.com/gatsbywang/… www.zhihu.com/question/49…