对于在C89,C90或C99的所有功能所需的原型? [英] Are prototypes required for all functions in C89, C90 or C99?

查看:156
本文介绍了对于在C89,C90或C99的所有功能所需的原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要真正符合标准,必须在C中的所有功能(除了主)有原型,即使他们自己在同一个翻译单元定义后,只用?

To be truly standards-compliant, must all functions in C (except for main) have a prototype, even if they are only used after their definition in the same translation unit?

推荐答案

这取决于你的意思是'真正符合标准的是什么。然而,简单的答案是这是一个好主意,以确保所有的功能在使用前有原型的范围。

It depends on what you mean by 'truly standards compliant'. However, the short answer is "it is a good idea to ensure that all functions have a prototype in scope before being used".

一个更合格的答卷指出,如果函数接受可变参数(特别是的printf()函数家族),那么原型必须在范围上要严格标准兼容。这是C89(从ANSI)和C90的真实(在ISO;同为C89除了部分编号)。除了可变参数的功能,但是,它返回一个int函数没有被宣布,而返回其他东西比一个int功能确实需要一个声明,表示返回类型,但不需要的原型参数列表。

A more qualified answer notes that if the function accepts variable arguments (notably the printf() family of functions), then a prototype must be in scope to be strictly standards compliant. This is true of C89 (from ANSI) and C90 (from ISO; the same as C89 except for the section numbering). Other than 'varargs' functions, though, functions which return an int do not have to be declared, and functions that return something other than an int do need a declaration that shows the return type but do not need the prototype for the argument list.

请注意,但是,如果该函数使用受到​​在没有原型的正常促销参数(例如,需要一个功能的字符 - 这两者都转换为 INT ;更严重的是,也许,一个函数,一个浮动,而不是双击),则需要一个原型。该标准不严这个让老C code至在标准一致性的编译器编译;旧的code未写入担心确保功能在使用前声明 - 根据定义,老年code没有使用原型,因为他们没有成为在C语言中,直到有一个标准

Note, however, that if the function takes arguments that are subject to 'normal promotions' in the absence of prototypes (for example, a function that takes a char or short - both of which are converted to int; more seriously, perhaps, a function that takes a float instead of a double), then a prototype is needed. The standard was lax about this to allow old C code to compile under standard conformant compilers; older code was not written to worry about ensuring that functions were declared before use - and by definition, older code did not use prototypes since they did not become available in C until there was a standard.

C99不允许'隐式int......这意味着这两个古怪的情况下,像静一; '(默认为一个int),也隐函数声明。这些在前进中提到(连同其它50重大变化),以ISO / IEC 9899:1999,
这是比较标准的previous版本:

C99 disallows 'implicit int'...that means both oddball cases like 'static a;' (an int by default) and also implicit function declarations. These are mentioned (along with about 50 other major changes) in the forward to ISO/IEC 9899:1999, which compares that standard to the previous versions:


      
  • 删除隐含 INT 结果
      ...

  •   
  • 删除隐函数声明

  •   

在ISO / IEC 9899:1990,§6.3.2.2函数调用说:

In ISO/IEC 9899:1990, §6.3.2.2 Function calls stated:

如果除权pression是precedes在函数调用括号参数列表由
  仅仅一个标识符。如果没有声明为此标识符可见,所述标识符是隐式
  声明完全一样,在包含函数调用最里面块,报关:

If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier. and if no declaration is visible for this identifier, the identifier is implicitly declared exactly as if, in the innermost block containing the function call, the declaration:

extern int identifier();


  
  

出现了。 38

38 也就是说。与块范围的标识符声明为有类型的函数外部链接不
  参数信息并返回一个 INT 。事实上,如果它没有被定义为有型的功能
  返回 INT 的行为是不确定的。

38 That is. an identifier with block scope declared to have external linkage with type function without parameter information and returning an int. If in fact it is not defined as having type "function returning int," the behavior is undefined.

这一段是缺少在1999年的标准。我没有(还)追踪,让中措辞的改变静一; 在C90和禁止它(要求静态int类型的; )。

This paragraph is missing in the 1999 standard. I've not (yet) tracked the change in verbiage that allows static a; in C90 and disallows it (requiring static int a;) in C99.

请注意,如果一个函数是静态的,它可在使用之前定义的,且不必发表的声明pceded $ P $。 GCC可以说服惠若非静态函数时没有声明preceding它定义( -Wmissing的原型)。

Note that if a function is static, it may be defined before it is used, and need not be preceded by a declaration. GCC can be persuaded to witter if a non-static function is defined without a declaration preceding it (-Wmissing-prototypes).

这篇关于对于在C89,C90或C99的所有功能所需的原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆