为什么省略明确的'廉政'类型的参数无法在GCC编译器有时会? [英] Why does omitting explicit 'int' type for a parameter fail to compile in gcc sometimes?

查看:85
本文介绍了为什么省略明确的'廉政'类型的参数无法在GCC编译器有时会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在声明中C变量有时可以省略的类型,如果你想声明 INT

When declaring variables in C you can omit the type sometimes if you want to declare an int.

为什么省略明确的'廉政'类型的参数无法在与其他非INT参数GCC编译器,除非在K&放申报; R风格

Why does omitting explicit 'int' type for a parameter fail to compile in gcc with other non-int parameters, unless declared in the K&R style?

这code产生一个​​错误:

This code generates an error:

main(argc, char *argv[])
{
  /* . . . */
}

通过下面的输出:

$gcc XXX.c -oXXX
XXX.c:X:X: error: expected ‘)’ before ‘char’
 main(argc, char *argv[])
            ^

不过,如果我写的K&安培; R风格的类型,我可以省略指定参数的 INT 键入第一个参数:

main(argc, argv)
char *argv[];
{
  /* . . . */
}

这编译罚款。

我怀疑原因是,使第一C标准时,他们决定,K&放大器; R自动-INT表示法应该是从为函数参数的较新的语法完全独立,特别是因为由时间标准正在被拉在一起自动-INT符号很可能已经被认为是符号差。

I suspect the reason is that when making the first C standard they decided that the K&R automatic-int notation should be completely seperate from the newer syntax for function parameters, especially since by the time the standard was being pulled together the automatic-int notation was likely already considered poor notation.

我对这些规则的兴趣是学术,我一般不写这个老C风格。

My interest in these rules is academic, I don't generally write this old-style C.

推荐答案

有在C两种形式的函数定义:K&安培; R风格和现代风格的原型。你不能在一个单一的定义混合。

There are two forms of function definition in C: K&R style and the modern style with prototypes. You cannot mix them in a single definition.

在K&安培; R风格(即Kernighan的&放的1978年第一版使用的样式; Ritchie的C程序设计语言,对语言的第一个正式的ANSI标准的11年前出版),你可以写:

In K&R style (i.e., the style used in the 1978 first edition of Kernighan & Ritchie's "The C Programming Language", published 11 years before the first official ANSI standard for the language), you could write:

/* Valid in K&R and 1989 ANSI C, invalid in C99 and later */
main(argc, argv)    
char *argv[];
{
    /* . . . */
}

在括号中的东西可以的只有的是标识符(可能为空)序列,该参数的名称。之间的 {,你可以选择性地具有参数声明的序列,并指定其类型。如果你省略了参数的类型,或者本身的功能,这将默认为 INT

The stuff between the parentheses can only be a (possibly empty) sequence of identifiers, the names of the parameters. Between the ) and { you could optionally have a sequence of parameter declarations, specifying their types. If you omitted the type of a parameter, or of the function itself, it would default to int.

1989年的ANSI C标准一直在这个古老的形式向后兼容,但宣布它为过时。 (不幸的是,恕我直言,它仍然是这样,即使在2011年的ISO C标准。)

The 1989 ANSI C standard kept this old form for backward compatibility, but declared it to be obsolescent. (Unfortunately, IMHO, it's remained that way even in the 2011 ISO C standard.)

1999年的ISO C标准放弃了隐式int类型的规则,因此,即使由于一些奇怪的原因,你想使用旧式定义,你仍然必须明确给出所有的类型:

The 1999 ISO C standard dropped the "implicit int" rule, so even if, for some odd reason, you wanted to use an old-style definition, you'd still have to give all the types explicitly:

 /* Valid in all versions of C, but obsolescent */
int main(argc, argv)
int argc;
char *argv[];
{
    /* ... */
}

一个现代化的原型定义是:

A modern prototype definition for main would be:

/* Valid in 1989 ANSI C and later; write it this way! */
int main(int argc, char *argv[]) { 
    /* ... */
}

您应该总是使用的原型;有没有好的理由写一个旧式函数定义的(除非您使用的是很旧的编译器卡住了,但它已经变得很难找到这样的编译器)。

You should always use prototypes; there is no good reason to write an old-style function definition (unless you're stuck using a very old compiler, but it has become very difficult even to find such a compiler).

这篇关于为什么省略明确的'廉政'类型的参数无法在GCC编译器有时会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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