我为什么要在C中声明一个函数? [重复]

时间:2022-09-10 23:18:47

This question already has an answer here:

这个问题在这里已有答案:

I have two source files test1.c and test2.c.

我有两个源文件test1.c和test2.c。

In test1.c:

在test1.c中:

#include <stdio.h>

void main() {
    checks(); }

In test2.c:

在test2.c中:

#include <stdio.h>

void checks(){
    printf("This is a sample Text");
    }

In this case I can successfully build and run this program.

在这种情况下,我可以成功构建和运行此程序。

So why should I use:

那我为什么要用:

void checks();

To declare the function?

要声明这个功能?

It seems perfectly fine now.

现在好像很好。

I am Using C99.

我正在使用C99。

1 个解决方案

#1


2  

In your case, the check() function has a very simple prototype and the default prototype applied by the C compiler is to accept anything as argument and to return an int. It is probably what is done here (except that, as you do not store the result of the function it is optimized out without noticing it).

在您的情况下,check()函数有一个非常简单的原型,C编译器应用的默认原型是接受任何作为参数并返回一个int。这可能是在这里做的(除了因为你没有存储函数的结果,它被优化而没有注意到它)。

If you want to check my theory, try to write this (and it should work until it reaches the linking phase):

如果你想检查我的理论,试着写这个(它应该工作,直到它到达链接阶段):

int result = check();

At the end, your code work because the linker finally find something that work to plug for the check() function (yet, it should still expect an int at some point).

最后,你的代码工作,因为链接器最终找到了一些可以插入check()函数的东西(但是,它仍然应该在某个时候期望一个int)。

In fact, the declaration of a function prototype is only useful in two cases:

事实上,函数原型的声明仅在两种情况下有用:

  1. The code of the function and the use of the function are in the same file.

    函数的代码和函数的使用在同一个文件中。

    When you use the function before its declaration (code source), then you need to tell the compiler what to expect when trying to statically type the function you are writing (the compiler read a source code file from top to bottom).

    当您在声明之前使用该函数(代码源)时,您需要告诉编译器在尝试静态键入您正在编写的函数时会发生什么(编译器从上到下读取源代码文件)。

    For example:

    例如:

    int bar (int a, int b, bool c);
    
    int foo (int a, bool b) {
        int result = bar (a, a, c);
        ...
    }
    
    int bar (int a, int b, bool c) {
        ...
     }
    
  2. The code of the function the use of the function are not in the same file.

    使用该函数的函数代码不在同一个文件中。

    Then, you usually get the definition of the function through a header file which collect all the information needed by the compiler to know how to statically type your code. The header file (*.h) contains all the prototypes of the functions of the module you are using. The implementation of the functions will come after at linking time.

    然后,您通常通过头文件获取函数的定义,该头文件收集编译器所需的所有信息,以了解如何静态键入代码。头文件(* .h)包含您正在使用的模块的所有函数原型。函数的实现将在链接时间之后进行。

Note that, I usually try to avoid the first case because it is really not logical. When you read a source code, you go from top to bottom, just as the compiler do, and you expect to find the function definition before its usage... So, it is much more logical to structure your code in a way that do not need to require such artifacts. In my humble opinion...

请注意,我通常会尝试避免第一种情况,因为它确实不合逻辑。当您阅读源代码时,就像编译器一样,从上到下,您希望在使用之前找到函数定义...因此,以一种方式构造代码更合​​乎逻辑不需要这样的工件。在我的愚见...

#1


2  

In your case, the check() function has a very simple prototype and the default prototype applied by the C compiler is to accept anything as argument and to return an int. It is probably what is done here (except that, as you do not store the result of the function it is optimized out without noticing it).

在您的情况下,check()函数有一个非常简单的原型,C编译器应用的默认原型是接受任何作为参数并返回一个int。这可能是在这里做的(除了因为你没有存储函数的结果,它被优化而没有注意到它)。

If you want to check my theory, try to write this (and it should work until it reaches the linking phase):

如果你想检查我的理论,试着写这个(它应该工作,直到它到达链接阶段):

int result = check();

At the end, your code work because the linker finally find something that work to plug for the check() function (yet, it should still expect an int at some point).

最后,你的代码工作,因为链接器最终找到了一些可以插入check()函数的东西(但是,它仍然应该在某个时候期望一个int)。

In fact, the declaration of a function prototype is only useful in two cases:

事实上,函数原型的声明仅在两种情况下有用:

  1. The code of the function and the use of the function are in the same file.

    函数的代码和函数的使用在同一个文件中。

    When you use the function before its declaration (code source), then you need to tell the compiler what to expect when trying to statically type the function you are writing (the compiler read a source code file from top to bottom).

    当您在声明之前使用该函数(代码源)时,您需要告诉编译器在尝试静态键入您正在编写的函数时会发生什么(编译器从上到下读取源代码文件)。

    For example:

    例如:

    int bar (int a, int b, bool c);
    
    int foo (int a, bool b) {
        int result = bar (a, a, c);
        ...
    }
    
    int bar (int a, int b, bool c) {
        ...
     }
    
  2. The code of the function the use of the function are not in the same file.

    使用该函数的函数代码不在同一个文件中。

    Then, you usually get the definition of the function through a header file which collect all the information needed by the compiler to know how to statically type your code. The header file (*.h) contains all the prototypes of the functions of the module you are using. The implementation of the functions will come after at linking time.

    然后,您通常通过头文件获取函数的定义,该头文件收集编译器所需的所有信息,以了解如何静态键入代码。头文件(* .h)包含您正在使用的模块的所有函数原型。函数的实现将在链接时间之后进行。

Note that, I usually try to avoid the first case because it is really not logical. When you read a source code, you go from top to bottom, just as the compiler do, and you expect to find the function definition before its usage... So, it is much more logical to structure your code in a way that do not need to require such artifacts. In my humble opinion...

请注意,我通常会尝试避免第一种情况,因为它确实不合逻辑。当您阅读源代码时,就像编译器一样,从上到下,您希望在使用之前找到函数定义...因此,以一种方式构造代码更合​​乎逻辑不需要这样的工件。在我的愚见...