函数声明和定义的过时形式Obsolete Forms of Function Declarations and Definitions

旧式函数声明和定义使用与 ANSI C 标准建议的语法略微不同的规则来声明参数。The old-style function declarations and definitions use slightly different rules for declaring parameters than the syntax recommended by the ANSI C standard. 首先,旧式声明不具有参数列表。First, the old-style declarations don't have a parameter list. 第二,在函数定义中,列出了参数,但未在参数列表中声明其类型。Second, in the function definition, the parameters are listed, but their types are not declared in the parameter list. 类型声明在构成函数主体的复合语句之前。The type declarations precede the compound statement constituting the function body. 该旧式语法已过时,不应在新代码中使用。The old-style syntax is obsolete and should not be used in new code. 但仍支持使用旧式语法的代码。Code using the old-style syntax is still supported, however. 此示例阐释声明和定义的过时形式:This example illustrates the obsolete forms of declarations and definitions:

double old_style();           /* Obsolete function declaration */  

double alt_style( a , real )  /* Obsolete function definition */  
    double *real;   
    int a;   
{  
    return ( *real + a ) ;  
}  

返回与 int 的大小相同的整数或指针的函数不需要具有声明,但建议具有声明。Functions returning an integer or pointer with the same size as an int are not required to have a declaration although the declaration is recommended.

为了遵循 ANSI C 标准,使用省略号的旧式函数声明现在会在使用 /Za 选项进行编译是生成错误,并在使用 /Ze 进行编译时生成 4 级别警告。To comply with the ANSI C standard, old-style function declarations using an ellipsis now generate an error when compiling with the /Za option and a level 4 warning when compiling with /Ze. 例如:For example:

void funct1( a, ... )  /* Generates a warning under /Ze or */  
int a;                 /* an error when compiling with /Za */  
{  
}  

您应将此声明重写为原型:You should rewrite this declaration as a prototype:

void funct1( int a, ... )  
{  
}  

旧式函数声明也会生成警告(如果您随后声明或定义具有省略号或具有与其提升的类型不同的类型的参数的相同函数)。Old-style function declarations also generate warnings if you subsequently declare or define the same function with either an ellipsis or a parameter with a type that is not the same as its promoted type.

下一节(C 函数定义)显示函数定义的语法(包括旧式语法)。The next section, C Function Definitions, shows the syntax for function definitions, including the old-style syntax. 旧式语法中的参数列表的非终止符是 identifier-list。The nonterminal for the list of parameters in the old-style syntax is identifier-list.

另请参阅See Also

函数概述Overview of Functions