什么是“静态"?在C中起作用? [英] What is a "static" function in C?

查看:78
本文介绍了什么是“静态"?在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,比如说a.c(没有a.h)并包含"a.c"-我得到"print_matrix@@....) already defined in 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?

更新为了澄清问题-正如许多人所指出的那样,我知道包含.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆