简体   繁体   English

具有静态局部变量的C99静态内联函数

[英]C99 static inline function with static local variable

If I write something like this in a C99 header: 如果我在C99标头中写这样的内容:

static inline void f()
{
    static int x = 0;
    // Do something with x
}

Is it guaranteed that each module including this header gets a separate instantiation of f() and its own instantiation of x ? 是否保证每个包含此标头的模块都获得f()的单独实例化和x自身实例化?

Yes, by definition, that's what static means. 是的,顾名思义,这就是static含义。 The fact that it's in a header is irrelevant. 它在标题中的事实是无关紧要的。 Conceptually, the C preprocessor creates one file to compile per .c file as if you had just (re)typed all the text in the header into the .c yourself. 从概念上讲,C预处理器将创建一个文件以针对每个.c文件进行编译,就好像您自己只是将标题中的所有文本(重新)键入到.c一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM