什么是“静态"?C中的函数? [英] What is a "static" function in C?
问题描述
问题是关于简单的 c 函数,不是 c++ static
方法,如注释中所述.
The question was about plain c functions, not c++ static
methods, as clarified in comments.
我了解 static
变量是什么,但什么是 static
函数?
I understand what a static
variable is, but what is a static
function?
为什么如果我声明一个函数,比方说void print_matrix
,比方说ac
(没有ah
)并包括"ac"
- 我得到 "print_matrix@@....) 已经在 a.obj"
中定义,但是如果我将它声明为 static void print_matrix
然后编译?
And why is it that if I declare a function, let's say void print_matrix
, in let's say a.c
(WITHOUT a.h
) and include "a.c"
- I get "print_matrix@@....) already defined in a.obj"
, BUT if I declare it as static void print_matrix
then it compiles?
UPDATE 只是为了澄清问题 - 正如你们许多人指出的那样,我知道包含 .c
是不好的.我只是为了暂时清除 main.c
中的空间,直到我更好地了解如何将所有这些函数分组到正确的 .h
和 .c
文件.只是一个临时的、快速的解决方案.
UPDATE Just to clear things up - I know that including .c
is bad, as many of you pointed out. I just do it to temporarily clear space in main.c
until I have a better idea of how to group all those functions into proper .h
and .c
files. Just a temporary, quick solution.
推荐答案
static
函数是仅对同一文件中的其他函数可见的函数(更准确地说,相同的翻译单位).
static
functions are functions that are only visible to other functions in the same file (more precisely the same translation unit).
编辑:对于那些认为问题的作者意味着类方法"的人:由于问题被标记为 C
,他的意思是一个普通的旧 C 函数.对于 (C++/Java/...) 类方法,static
意味着可以在类本身上调用此方法,不需要该类的实例.
EDIT: For those who thought, that the author of the questions meant a 'class method': As the question is tagged C
he means a plain old C function. For (C++/Java/...) class methods, static
means that this method can be called on the class itself, no instance of that class necessary.
这篇关于什么是“静态"?C中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!