简体   繁体   English

在C中不使用函数原型有什么好处吗?

[英]Are there any benefits to NOT using function prototypes in C?

I'm working with some C code that does not contain function prototypes for a certain class of functions. 我正在使用一些不包含某类函数的函数原型的C代码。 Are there any advantages to not using function prototypes? 不使用函数原型有什么好处吗? The functions never call each other and have no parameters. 这些函数从不相互调用,也没有参数。 The code changes a lot, so maybe it's just one less line to edit? 代码变化很大,所以也许它只需要编辑一行?

Function prototypes are for external functions. 函数原型用于外部函数。 My rule is that every non-static function gets a prototype, except main(). 我的规则是每个非静态函数都获得一个原型,除了main()。 I use the '-Wmissing-prototypes' GCC option. 我使用'-Wmissing-prototypes'GCC选项。 Usually what it catches is when I forget to declare a function static. 通常它捕获的是我忘记声明一个静态函数。

Also, declare functions in C this way: 另外,用C语言声明函数:

void function(void);

And not this way: 而不是这样:

void function();

Because the second way means that the function takes an unspecified number of parameters, which isn't what you want (it's for compatibility with pre-ANSI C). 因为第二种方式意味着该函数采用了未指定数量的参数,这不是您想要的(它与ANSI C之前的兼容性)。

No .

And I don't know if it is even legal in strict ansi c. 而且我不知道它是否在严格的情况下是合法的。

"Code changes a lot" when applied to function prototypes is also a bad code smell. 应用于函数原型的“代码变化很大”也是一个糟糕的代码味道。 If the interface (function signature) changes a lot, the responsibilities of a function are probabaly not very clear. 如果接口(函数签名)发生了很大变化,那么函数的职责可能不太清楚。 First work to figure out how to divide the problem to subresponsibilities and only after that start to write code. 首先要弄清楚如何将问题划分为次要职责,然后才开始编写代码。

"so maybe it's just one less line to edit?" “所以也许这只是编辑的一条线?”

That's the only possible "benefit" in this case, plain laziness. 在这种情况下,这是唯一可能的“好处”,即懒惰。

Less code to change is the only 'advantage' I can think of. 更少的代码改变是我能想到的唯一“优势”。 Typically this is just 'lazy' 通常这只是'懒惰'

In any event, the disadvantages are more significant: you've gotta have the functions all in one source file; 无论如何,缺点更为重要:你必须在一个源文件中拥有所有功能; order of functions in the source file now matters, etc. Also, other people will be confused looking at/for the header file...best practice is to .c and .h it. 源文件中的函数顺序现在很重要等等。此外,其他人会看到/为头文件混淆...最佳实践是.c和.h它。

我看到的唯一好处是,每次更改函数本身时都不必更新函数原型。

The code changes a lot, so maybe it's just one less line to edit? 代码变化很大,所以也许它只需要编辑一行?

I'd guess that's the reason. 我猜这就是原因。 I can't think of any other reason - neither does the compilation speed change (practically), nor the execution time, merely the time for updating the code. 我想不出任何其他原因 - 编译速度(实际上)也没有改变,执行时间也没有,只是更新代码的时间。

The only advantage I can think of is that it saves you having to copy and paste the first line of the function into the prototypes section of your .c or .h file. 我能想到的唯一优势是它可以节省你必须将函数的第一行复制并粘贴到.c或.h文件的prototypes部分。

For functions which are referenced in other files, you have no choice but to have a prototype. 对于其他文件中引用的函数,您别无选择,只能拥有原型。

For functions with file scope (ie static functions), it's useful to have all the prototypes in a block at the top of the file. 对于具有文件范围的函数(即静态函数),将所有原型放在文件顶部的块中是有用的。 That way, any static function can be called by another from anywhere in that file. 这样,任何静态函数都可以从该文件中的任何位置调用。 Without prototypes, function A() can only call function B() if B() is declared above it in the code. 如果没有原型,函数A()只能在代码中声明B()之上调用函数B()。

Also, some compilers will make unsafe assumptions about parameters if there's no prototype in scope. 此外,如果范围内没有原型,一些编译器会对参数做出不安全的假设。

In addition, if you write code which has to conform to MISRA-C, it's a requirement that all functions have a prototype in scope. 此外,如果您编写的代码必须符合MISRA-C,则要求所有函数都具有范围内的原型。

I'd also advocate ensuring that the prototype contains the parameter names, not just their types (which is legal), as it clarifies the purpose of the parameters just by looking at the prototype. 我还提倡确保原型包含参数名称,而不仅仅是它们的类型(这是合法的),因为它只是通过查看原型来阐明参数的用途。

它打字较少,因此可能会降低您的RSI风险。

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

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