C语言 在 main 之前定义一个函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4096304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:54:48  来源:igfitidea点击:

Define a function before main?

cc99

提问by Neeladri Vishweswaran

Are function declarations/prototypes necessary in C99 ?

C99 中是否需要函数声明/原型?

I am currently defining my functions in a header file and #include-ING it in the main file. Is this OK in C99 ?

我目前正在头文件中定义我的函数,并在主文件中 #include-ING 它。这在 C99 中可以吗?

Why do most programmers declare/prototype the function before main() and define it after main() ? Isn't it just easier to define them before main and avoid all the declarations/prototypes ?

为什么大多数程序员在 main() 之前声明/原型化函数并在 main() 之后定义它?在 main 之前定义它们并避免所有声明/原型不是更容易吗?

Contents of header.h file:

header.h 文件的内容:

int foo(int foo)
{
// code
return 1;
}

Contents of main file:

主文件内容:

#include <stdio.h>

#include "header.h"

int main(void)
{
foo(1);
return 0;
}

回答by Matthieu

How and where to prototype and define a function in C :

如何以及在何处在 C 中原型化和定义函数:

  1. Your function is used only in a specific .c file : Define it static in the .c file. The function will only be visible and compiled for this file.

  2. Your function is used in multiple .c files : Choose an appropriate c file to host your definition (All foo related functions in a foo.c file for example), and have a related header file to have all non-static (think public) functions prototyped. The function will be compiled only once, but visible to any file that includes the header files. Everything will be put together at link time. Possible improvement : always make the related header file, the first one included in its c file, this way, you will be sure that any file can include it safely without the need of other includes to make it work, reference : Large Scale C++ projects(Most of the rules apply to C too).

  3. Your function is inlinable (are you sure it is ?) : Define the function static inline in an appropriate header file. The compiler should replace any call to your function by the definition if it is possible (think macro-like).

  1. 您的函数仅在特定的 .c 文件中使用:在 .c 文件中将其定义为静态。该函数将仅针对此文件可见和编译。

  2. 您的函数在多个 .c 文件中使用:选择一个合适的 c 文件来承载您的定义(例如,foo.c 文件中的所有 foo 相关函数),并具有相关的头文件以包含所有非静态(认为是公共的)函数原型。该函数将只编译一次,但对包含头文件的任何文件都是可见的。一切都将在链接时放在一起。可能的改进:始终制作相关的头文件,第一个包含在其 c 文件中,这样,您将确保任何文件都可以安全地包含它,而无需其他包含来使其工作,参考:大型 C++ 项目(大多数规则也适用于 C)。

  3. 您的函数是可内联的(您确定是吗?):在适当的头文件中定义静态内联函数。如果可能的话,编译器应该用定义替换对你的函数的任何调用(像宏一样)。

The notion of before-after another function (your main function) in c is only a matter of style. Either you do :

c 中的 before-after 另一个函数(您的主函数)的概念只是风格问题。要么你做:

static int foo(int foo) 
{ 
// code 
return 1; 
} 

int main(void) 
{ 
foo(1); 
return 0; 
} 

Or

或者

static int foo(int foo);

int main(void) 
{ 
foo(1); 
return 0; 
} 

static int foo(int foo)
{ 
// code 
return 1; 
} 

will result in the same program. The second way is prefered by programmers because you don`t have to reorganize or declare new prototypes every time you declare a new function that use the other ones. Plus you get a nice list of every functions declared in your file. It makes life easier in the long run for you and your team.

将导致相同的程序。程序员更喜欢第二种方式,因为您不必在每次声明使用其他原型的新函数时重新组织或声明新原型。另外,您可以获得文件中声明的每个函数的漂亮列表。从长远来看,它会让您和您的团队的生活更轻松。

回答by Puppy

People typically do it because it's easier to do with multiple files. If you declare in a header then you can just #include that header anywhere you need those functions. If you define them in a header and then include in another translation unit, bang.

人们通常会这样做,因为处理多个文件更容易。如果您在标题中声明,那么您可以在需要这些函数的任何地方#include 该标题。如果您在标题中定义它们,然后将它们包含在另一个翻译单元中,那么 bang。

回答by AnT

Function declarationsare required in C99. Function prototypesare not required in C99.

C99 中需要函数声明。C99 中不需要函数原型

Declaring functions before the point of the call and defining them after the point of the call is a popular approach to structuring the program code. However, this is in no way what the "most" programmers do. On the contrary, a more popular approach is to definefunction before the point of the first call, in which case the separate declaration is not necessary. This approach requires less maintenance, which is why it is more popular than what you describe.

在调用点之前声明函数并在调用点之后定义它们是构建程序代码的流行方法。然而,这绝不是“大多数”程序员所做的。相反,更流行的方法是在第一次调用之前定义函数,在这种情况下不需要单独声明。这种方法需要较少的维护,这就是它比您描述的更受欢迎的原因。

Separate declarations/definitions are normally used with external functions only, i.e. with functions used across several translation units. Such functions are declared in header files and defined in implementation files.

单独的声明/定义通常仅与外部函数一起使用,即与跨多个翻译单元使用的函数一起使用。此类函数在头文件中声明并在实现文件中定义。

回答by Christoph

You should only ever define inlinefunctions in headers. Although you can have extern inlinefunctions, the common case is static inline.

您应该只inline在标题中定义函数。虽然你可以有extern inline函数,但常见的情况是static inline.

Rule of thumb for header files:

头文件的经验法则:

  • function declarations should be extern
  • function definitions should be static inline
  • variable declarations should be extern
  • variable definitions should be static const
  • 函数声明应该是 extern
  • 函数定义应该是 static inline
  • 变量声明应该是 extern
  • 变量定义应该是 static const

As C. Ross asked for it, here's reasoning behind it: A resource with external linkage should only ever be defined once[1]. It follows that definitions should not reside in header files, which are intended to be included in more than one place.

正如 C. Ross 所要求的,其背后的原因如下:具有外部链接的资源应该只定义一次 [1]。因此,定义不应驻留在头文件中,头文件旨在包含在多个位置。

Having staticdefinitions in header files won't lead to any problems, but is generally frowned upon because the code has to be compiled more than once and will be present in different object files, which will increase the executable size (assuming the linker isn't smart enough to figure out the code duplication).

static在头文件不会导致任何问题的定义,但在因为代码必须被不止一次编译,将存在于不同的目标文件,这会增加可执行文件的大小一般不赞成(假设接头是不可足够聪明以找出代码重复)。

The common exceptions to this rule are constants and inlinefunctions, which are supposed to be visible to the compiler in each translation unit to make further optimizations possible.

此规则的常见例外是常量和inline函数,它们应该对每个翻译单元中的编译器可见,以便进一步优化。

Note:[1] This does not apply to inlinefunctions with external linkage, but as it's unspecified which of the multiple definitions of an inline function will be used in the evaluation of a function designator, they are mostly useless

注意:[1] 这不适用于inline具有外部链接的函数,但由于未指定内联函数的多个定义中的哪一个将用于函数指示符的评估,因此它们大多无用

回答by Karel Petranek

Your approach is fine for small programs. Header files are meant for declarations and constant definitions - they provide an interface to the program they "encapsulate". Headers are meant as an interface for other program units.

您的方法适用于小程序。头文件用于声明和常量定义——它们为它们“封装”的程序提供了一个接口。标头旨在作为其他程序单元的接口。

In case you have more .c files, forward declarations and header files are necessary, because a C function can be defined only once for the whole program (search for one definition rule), even though you may use the function anywhere (in any .c file). If you defined it in a header, it would get included in all .c files you use it in and result in multiple definitions.

如果你有更多的 .c 文件,前向声明和头文件是必要的,因为一个 C 函数只能为整个程序定义一次(搜索一个定义规则),即使你可以在任何地方使用该函数(在任何 .c 文件中)。 c 文件)。如果您在头文件中定义它,它将包含在您使用它的所有 .c 文件中并导致多个定义。

回答by peoro

It's quicker to do like that, but I personally prefer to have the main function at the beginning of the main file, and put the other functions in other files or below main.

这样做会更快,但我个人更喜欢将 main 函数放在主文件的开头,并将其他函数放在其他文件中或 main 之下。

Note that in your example you should avoid declaring foo() in a header file: you won't be able to include it in two different source files. Declare it in the C file containing main(); you won't need to define it elsewhere unless you're referring to it from some other files.

请注意,在您的示例中,您应该避免在头文件中声明 foo():您将无法将它包含在两个不同的源文件中。在包含 main() 的 C 文件中声明它;您不需要在其他地方定义它,除非您从其他一些文件中引用它。

回答by Prof. Falken contract breached

Yes, it is easier to define them before main. If you only want to use these functions from within the file, a prototype is not necessary. In that case however, you can also prepend the "static" keyword before the function definition. (In the C file.) That will ensure the function is not visible to other files. (At link time.)

是的,在 main 之前定义它们更容易。如果您只想在文件中使用这些函数,则不需要原型。但是,在这种情况下,您也可以在函数定义之前添加“static”关键字。(在 C 文件中。)这将确保该函数对其他文件不可见。(在链接时间。)

Do not put static keywords in include files.

不要将静态关键字放在包含文件中。

回答by Clifford

Why do most programmers declare/prototype the function before main() and define it after main() ?

为什么大多数程序员在 main() 之前声明/原型化函数并在 main() 之后定义它?

Merely because most humans read sequentially. Start a story from the beginning, not the middle. Not necessary, just intuitive.

仅仅是因为大多数人是按顺序阅读的。从头开始一个故事,而不是中间。没有必要,只是直观。

Of course if the code being prototyped is in a separate compilation unit, the prototypes arenecessary.

当然,如果被原型化的代码在一个单独的编译单元中,原型必要的。

回答by Clifford

You should always prototype.

你应该总是原型。

The reasons for this are;

这样做的原因是;

  1. methodical prototyping produces a succinct list in header files of the functions in the code - this is invaluable to future readers

  2. in anything but the simplest projects, many functions will not have visibility prior to main.

  3. main should be the first function in its file; it's easier for the reader, since we read down, not up

  1. 有条不紊的原型在代码中函数的头文件中生成一个简洁的列表 - 这对未来的读者来说是无价的

  2. 除了最简单的项目,许多函数在 main 之前是不可见的。

  3. main 应该是其文件中的第一个函数;这对读者来说更容易,因为我们是往下读,而不是往上读

回答by rmkyjv

It is always a good practice to declare the functions in either before main or in a separate header file which will be included in other c files where we have used that function. By doing this we can easily identify all the functions declared/defined in that .C or .H files. And we should use extern key word before declaring the function in header file.

在 main 之前或在单独的头文件中声明函数始终是一个好习惯,该头文件将包含在我们使用该函数的其他 c 文件中。通过这样做,我们可以轻松识别在该 .C 或 .H 文件中声明/定义的所有函数。并且我们应该在头文件中声明函数之前使用 extern 关键字。