gpt4 book ai didi

c - C中的静态局部变量是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-04 05:58:44 25 4
gpt4 key购买 nike

 BOOL foo(void){

static BOOL displayed = FALSE;
static BOOL initialized = FALSE;

if (displayed)
return FALSE;

//more code

displayed = TRUE;
return FALSE;
}

C 中静态局部变量的含义是什么?

如果第二次调用此方法,显示的不会重新初始化为FALSE吗?

最佳答案

静态局部变量只在程序启动前初始化一次。然后,它们的值在调用之间保持不变。

来自标准,第 6.2.4/3 节对象的存储持续时间:

An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

关于c - C中的静态局部变量是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22505797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com