gpt4 book ai didi

c - C 中的函数语法

转载 作者:太空宇宙 更新时间:2023-11-04 05:20:50 26 4
gpt4 key购买 nike

我是 C 的新手,我一直在研究别人的代码,我发现了这个函数:

static
cp_file(output, input, record, ft)
dy_char *output; /* output file */
dy_char *input; /* input file */
dy_char *record; /* record id */
int ft; /* file type */
{

这是否与这样说完全一样:

static cp_file(dy_char *output, dy_char *input, dy_char *record, int ft) {   

一个比另一个更高效,还是纯粹是一种不同的语法风格?如果它们不同,有什么区别?

最佳答案

不,它们不相同。

第一种形式是老式的函数定义,第二种形式是函数定义的原型(prototype)形式。

它们在参数传递转换方面有所不同。具有原型(prototype)的函数转换参数就像通过赋值一样,而非原型(prototype)函数就像您的第一个示例中那样执行默认参数提升。

旧形式的参数转换:

(C99, 6.5.2.2p6) "If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions."

原型(prototype)形式的参数转换:

(C99, 6.5.2.2p7) "If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type."

请注意,旧形式(非原型(prototype))已过时,强烈建议不要使用。

(C99, 6.11.7p1 Future languages directions) "The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature."

关于c - C 中的函数语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9520560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com