静态存储类说明符static Storage-Class Specifier

使用 static 存储类说明符在内部级别声明的变量具有全局生存期,但它仅在声明它的块中可见。A variable declared at the internal level with the static storage-class specifier has a global lifetime but is visible only within the block in which it is declared. 对于常量字符串,使用 static 会很有用,因为它减少了频繁初始化经常调用的函数的开销。For constant strings, using static is useful because it alleviates the overhead of frequent initialization in often-called functions.

备注Remarks

如果不显式初始化 static 变量,则默认情况下它将初始化为 0。If you do not explicitly initialize a static variable, it is initialized to 0 by default. 在函数内,static 会导致存储被分配并用作定义。Inside a function, static causes storage to be allocated and serves as a definition. 内部静态变量仅提供对单个函数可见的私有永久存储。Internal static variables provide private, permanent storage visible to only a single function.

请参阅See Also

C 存储类C Storage Classes
存储类 (C++)Storage classes (C++)