简体   繁体   English

在main之前定义一个函数?

[英]Define a function before main?

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. 我目前正在头文件中定义我的函数,并在主文件中定义#include-ING。 Is this OK in C99 ? 在C99中可以吗?

Why do most programmers declare/prototype the function before main() and define it after main() ? 为什么大多数程序员在main()之前声明/原型化函数并在main()之后定义它? Isn't it just easier to define them before main and avoid all the declarations/prototypes ? 是不是更容易在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;
}

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. 您的函数仅在特定的.c文件中使用:在.c文件中将其定义为静态。 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. 您的函数用于多个.c文件:选择一个适当的c文件来托管您的定义(例如,在foo.c文件中所有与foo相关的函数),并且有一个相关的头文件以使所有非静态(想想公共)函数原型。 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). 可能的改进:总是制作相关的头文件,第一个包含在其c文件中,这样,您将确保任何文件都可以安全地包含它而不需要其他包含使其工作,参考: 大型C ++项目 (大多数规则也适用于C)。

  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). 如果可能的话,编译器应该通过定义替换对函数的任何调用(想像宏一样)。

The notion of before-after another function (your main function) in c is only a matter of style. c中前后另一个函数(你的主函数)的概念只是风格问题。 Either you do : 要么:

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. 从长远来看,它可以让您和您的团队更轻松。

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. 如果您在标题中声明,那么您可以在需要这些功能的任何地方#include该标题。 If you define them in a header and then include in another translation unit, bang. 如果你在标题中定义它们然后包含在另一个翻译单元中,那就是bang。

You should only ever define inline functions in headers. 您应该只在标头中定义inline函数。 Although you can have extern inline functions, the common case is static inline . 虽然您可以使用extern inline函数,但常见的情况是static inline

Rule of thumb for header files: 头文件的经验法则:

  • function declarations should be extern 函数声明应该是extern
  • function definitions should be static inline 函数定义应该是static inline
  • variable declarations should be extern 变量声明应该是extern
  • variable definitions should be static const 变量定义应该是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]. 正如C.罗斯所要求的那样,这背后的理由是:具有外部联系的资源应该只定义一次[1]。 It follows that definitions should not reside in header files, which are intended to be included in more than one place. 因此,定义不应存在于头文件中,头文件应包含在多个位置。

Having static definitions 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 inline functions, 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 inline functions 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函数,但由于未指定内联函数的多个定义中的哪一个将用于评估函数指示符,因此它们大多无用

Function declarations are required in C99. C99中需要函数声明 Function prototypes are not required in 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 define function 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, ie with functions used across several translation units. 单独的声明/定义通常仅与外部函数一起使用,即在多个翻译单元中使用的函数。 Such functions are declared in header files and defined in implementation files. 这些函数在头文件中声明并在实现文件中定义。

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

Yes, it is easier to define them before main. 是的,在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. 但是,在这种情况下,您还可以在函数定义之前添加“static”关键字。 (In the C file.) That will ensure the function is not visible to other files. (在C文件中。)这将确保该功能对其他文件不可见。 (At link time.) (在链接时。)

Do not put static keywords in include files. 不要在包含文件中放置静态关键字。

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. 这样做更快,但我个人更喜欢在主文件的开头有主函数,并将其他函数放在其他文件或主文件下面。

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. 请注意,在您的示例中,您应该避免在头文件中声明foo():您将无法将其包含在两个不同的源文件中。 Declare it in the C file containing main(); 在包含main()的C文件中声明它; you won't need to define it elsewhere unless you're referring to it from some other files. 除非你从其他文件中引用它,否则你不需要在别处定义它。

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. 除了最简单的项目之外,许多函数在main之前都没有可见性。

  3. main should be the first function in its file; main应该是其文件中的第一个函数; it's easier for the reader, since we read down, not up 这对读者来说更容易,因为我们读下来,而不是起来

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 are necessary. 当然,如果原型的代码是在一个单独的编译单元中,原型必要的。

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. 在main之前或单独的头文件中声明函数总是一个好习惯,它将包含在我们使用该函数的其他c文件中。 By doing this we can easily identify all the functions declared/defined in that .C or .H files. 通过这样做,我们可以轻松识别在.C或.H文件中声明/定义的所有函数。 And we should use extern key word before declaring the function in header file. 在声明头文件中的函数之前,我们应该使用extern关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM