正确声明在ANSI C的main()函数 [英] correctly declaring the main() function in ANSI C
问题描述
C标准说:
称为在程序启动功能
被命名为主力。实施
声明没有原型,这
功能。它应与被定义
返回一个int类型,并没有
参数:INT主要(无效){/ * ... * /}
或具有两个参数(称为
这里为argc和argv,虽然任何
名称也可以使用,因为它们是本地
于它们是功能
声明):INT主(INT ARGC,CHAR *的argv []){/ *
... * /}
或等同或在一些其他
实现定义的方式。
块引用>不过,Kernighan的&安培;里奇在他们的第二版(规范ANSI C)圣经只需使用:
的main()
{
/ * taram pampam ... * / 返回0;
}谁是谁非?
是否有做功能,无需返回值自动承担在返回INT
用C?解决方案好吧,如果你想ANSI C,然后通过定义的标准是正确的。
在C89 / C90的
INT
返回类型是隐含的,所以K&放大器;:R的定义是可以接受的。在C99这已不再是这种情况。
在C90标准有以下措辞(5.1.2.2.1程序启动),这是非常相似的C99措辞(可能是最显著它使用了更少的强可以而不是必须):
称为在程序启动时的功能被命名为
主
。实施声明没有原型此功能。它可以不带参数进行定义:INT主要(无效){/ * ... * /}
或两个参数(这里称为
ARGC
和的argv
,尽管任何名字都可以使用,如他们是当地在声明它们的功能):INT主(INT ARGC,CHAR *的argv []){/ * ... * /}
如果它们被定义,参数为
主
函数应当遵守下列限制:
[等等。 ...]
块引用>有没有在这部分是直接有关的事实,离开了返回类型,将导致其默认为
INT
。坦率地说,我有一个很难找到确切位置行为是标准规定。我能来最接近的是6.7.1(函数的定义),其中对于函数定义语法表明,宣言 - 符是可选的,例子说:
例如:
在以下几点:
的extern INT MAX(int类型的,INT B)
{
返回> B' A:B;
}
的extern
是存储类说明和INT
是类型说明符(每个可以省略这些是默认的)...
块引用>The C standard say:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent or in some other implementation-defined manner.
However, Kernighan & Ritchie in their second edition (the canonical ANSI C) bible just use:
main() { /* taram pampam ... */ return 0; }
Who is right? Does it have to do with function without return value automatic assume to be returning
int
in C?解决方案Well, if you want ANSI C, then by definition the standard is right.
In C89/C90 the
int
return type is implied, so the K&R definition would be acceptable.In C99 this is no longer the case.
The C90 standard has the following wording (5.1.2.2.1 Program startup), which is very similar to the C99 wording (probably most significantly it uses the less strong 'can' instead of 'shall'):
The function called at program startup is named
main
. The implementation declares no prototype for this function. It can be defined with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used, as they are local to the function in which they are declared):int main(int argc, char *argv[]) { /* ... */ }
If they are defined, the parameters to the
main
function shall obey the following constraints:[etc. ...]
There's nothing in that section directly about the fact that leaving off the return type will result in it defaulting to
int
.Frankly, I have a hard time finding exactly where that behavior is specified by the standard. The closest I can come is in 6.7.1 (Functions definitions) where the grammar for function definitions indicates that the 'declaration-specifiers' are optional, and the examples say:
Examples:
In the following:
extern int max(int a, int b) { return a > b ? a : b; }
extern
is the storage class specifier andint
is the type specifier (each of which may be omitted as those are the defaults)...
这篇关于正确声明在ANSI C的main()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!