当前位置:网站首页>The properties of variables and functions beginning with static specifiers in C language
The properties of variables and functions beginning with static specifiers in C language
2022-07-22 07:06:00 【Little flying general】
When to use static As a prefix to variables ?
If used static specifier , Then the first external declaration of the identifier will make the changed identifier have an internal connection , The identifier of the internal link is only in the current .c Source file (transilation Unit, Translation unit ) so , That is, only in the current .c The source file is visible , Other source files are not visible .
Declared global variables , If only in the current .c file (transilation Unit, Translation unit ) Use in , Not elsewhere .c Use... In the document , At this time in .c instead of .h Used in documents static Statement ; If the declared global variables are elsewhere .c Use... In the document , Then declare it as extern Of , And put it on .h In file . except extern The external variables of are declared in the header file , Do not declare any variables in the header file . Otherwise, when expanding the opening file in the source file , There may be multiple variable declarations . Don't define variables in the header file .
Reference resources Stack Overflow:https://stackoverflow.com/questions/1856599/when-to-use-static-keyword-before-global-variables
When to use static As a prefix to a function ?
Declared global functions , If only in the current .c Use... In the document , Don't want external access , The statement is static type .
C The language function definition defaults to global , And the statement is static The function of is only in the current .c It can be seen in the document , other .c The file is not visible ; It can be in other .c Redefine the function with the same name in the file .
example:
static_fun.h
#pragma once
#include <stdio.h>
static void static_fun();
void fun();
void printfun();
static_fun.c
#include "static_fun.h"
static int static_variable = 1;
static void static_fun() {
printf("Static fun call\n");
}
void fun() {
printf("Global fun call\n");
static_fun();
}
void printfun() {
printf("static variable is: %d\n", static_variable);
}
main.c
#include "static_fun.h"
int main()
{
// static function/variable call test
static_fun(); // error: static function decleared but not defined
fun(); // OK: extern function
printf("Static variable: %d\n", static_variable); // error: static_variable not defined
printfun(); // OK: access static variable by extern function
return 0;
}
static_fun() Only in static_fun.c You can see , therefore main.c in static_fun(); The call is treated as a declaration , But the function definition was not found ;
printf static_variable The static variable of is only in static_fun.c You can see , So the variable is undefined ;
You can access static variables or functions indirectly through external functions .
static This feature of keywords is C Languages are often used to design and implement ADT (Abstract Data Type), Such as the list , Abstract data structures such as trees .static The declared functions and variables are only in ADT It's visible inside , External inaccessible .
void func() {
static int val = 1;
++val;
printf("static value: %d\n", val);
}
int main()
{
func();
func();
return 0;
}
static value: 2
static value: 3
Two calls contain static Function of , The second definition did not take effect , Nor did static variable reinitialization occur , The program executes to the static variable , Its value is still the result of the last time .
边栏推荐
猜你喜欢
ArcGIS像元大小从0.00025变为米
Jmeter安装与设置(一)
EventLoop事件循环机制
js类的创建和继承
JMeter installation and setting (I)
Matlab GUI programming skills (VIII): uitoolbar create toolbar in the drawing window
Matlab GUI programming skills (XI): axes/geoaxes/polaraxes drawing to create GUI coordinate area
win10下载地址
[matlab problem solving] solve the problem after matlab compilation Exe file cannot run on another computer
Observer mode and publish / subscribe mode
随机推荐
C#字体使用效果
PS Xiaobai starts from 0
Foundation of Mathematics: Lagrangian optimization and duality
Matlab GUI programming skills (VIII): uitoolbar create toolbar in the drawing window
C# out 关键字error CS0136 无法在此范围中声明该局部变量或参数,因为该名称在封闭局部范围中用于定义局部变量或参数
自定义 Handler 时如何有效地避免内存泄漏问题?
Dangling pointer and orphan memory
Is online stock account opening complicated? Excuse me, is it safe to open a stock account by mobile phone?
vscode - 所选环境中没有可用的Pip安装程序
策略模式
Jupyter使用TIPS
三星Galaxy Fold拆解:内部极其复杂,铰链成屏幕损坏主因?
查找表中重复内容
自定义类加载器实现
高通与苹果和解,获得至少45亿美元和解费!下一个目标:华为!
接口与抽象类
win10下载地址
请问开户对年龄有限制么?请问,手机开户股票开户安全吗?
ENVI波段合成逆运算——波段拆分
String说明