首页 > 解决方案 > 使用带有两个单独文件的函数

问题描述

文件:A(主要),B

我了解到如果没有函数定义,B 的函数就不能在 A 中使用。

但是我的代码在没有函数定义的 A、B 文件中正常运行

这是我的代码:

公元前

void a()
{
    printf("hi");
}

交流电

#include <stdio.h>
void main()
{
    a();
}

它是什么?我很困惑。

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ</p>

附录...对不起我的坏问题。我的代码可以很好地处理错误。但我看不到错误。

但我对 'void a();' 之间的区别有更多疑问 '外部无效a();'

标签: c

解决方案


Once upon a time, C did not require prior declaration of all functions. Many compilers still let you get away with this.

In file A.c, when you called

a();

where a was a function the compiler has never seen before, the compiler assumed that the declaration

extern int a();

was in scope. That is, the compiler assumed that a was a function taking unspecified arguments and returning int.

Or, that used to be the rule. That rule is no longer in C, so yes, you are supposed to explicitly declare all your functions before you call them. Most of today's compilers will warn you when they apply the old rule, and many aren't willing to apply the rule at all, or at least, not unless you use a non-default option flag requesting them to. But it sounds like your compiler is still willing to apply the rule without warning or error. That's great if you're compiling a bunch of very old code, but it's not so great if you're trying to learn modern C.

Now, in this case you have the additional problem that the actual definition of function a in file B.c defines it as returning void, not int, so theoretically that's wrong, too. But, in practice, the error of misdeclaring (or mis-calling) void- versus int-returning functions is an innocuous one, that doesn't cause any real problems. (It's still wrong, though, and worth avoiding.)

I think you know this, but a correct setup would either be to have file A.c look like this:

#include <stdio.h>
extern void a(void);
int main()
{
    a();
}

or else to create the file B.h containing

extern void a(void);

and then to have file A.c look like this:

#include <stdio.h>
#include "B.h"
int main()
{
    a();
}

(Note that I have also changed void main() to int main(), for correctness. If you're using an old compiler, as it sounds like you are, you may also have to add the line return 0; at the end of main().)


Addendum. You had also asked about that extern keyword. It has to do with the distinction between declarations and definitions. But this distinction plays out slightly differently for functions, versus global variables.

Declarations explain what type something has. Definitions explain what type something has, and they additionally allocate memory for the something, and supply its initial value.

These are declarations:

extern int i;
int f(int);
extern int f2(int, double);

These are definitions:

int i;
int i2 = 2;
int f(int x) { return 2 * x; }
int f2(int n, double x) { return n * x; }

The keyword extern explicitly says, "This is a declaration, the definition is somewhere else." For global variables, this makes a big difference. But for functions, when you say int f(int);, the compiler can tell, when it finds a , instead of a {, that this is a declaration (not a definition), so the keyword extern is optional in function declarations.

(Also, functions are always global in C; there are no local functions.)

See also section 4.2 and section 4.3 of these course notes.


推荐阅读