简体   繁体   English

为什么具有空参数列表的函数原型与具有char参数的函数原型冲突?

[英]Why does a function prototype with an empty argument list conflicts with one that has a char argument?

With the code below, both clang and gcc called with -std=c11 complain about conflicting types for foo. 使用下面的代码,使用-std = c11调用的clang和gcc都会抱怨foo的冲突类型。

int foo();

int main(void) {
  return foo(0);
}

int foo(char a) {
   return a;
}

According to the answer in https://stackoverflow.com/a/13950800/1078224 , in (older?) C standards the type int has been assumed when no variable type was given. 根据https://stackoverflow.com/a/13950800/1078224中的答案,在(较旧的?)C标准中,当没有给出变量类型时,假设类型为int。 However, the C11 standard draft ( http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf ), section 6.7.6.3, $14 says that 但是,C11标准草案( http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf ),第6.7.6.3节,14美元表示

The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied. 函数声明符中的空列表不是该函数定义的一部分,它指定不提供有关参数数量或类型的信息。

From this I conclude that the code above should be actually valid. 由此我得出结论,上面的代码实际上应该是有效的。 Or am I missing some other relevant section of the standard? 或者我错过了标准的其他相关部分?

C 2011 (N1570) 6.7.6.3 15: C 2011(N1570)6.7.6.3 15:

For two function types to be compatible, both shall specify compatible return types. 要使两种函数类型兼容,两者都应指定兼容的返回类型。 Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; 此外,参数类型列表(如果两者都存在)应在参数的数量和省略号终止符的使用中一致; corresponding parameters shall have compatible types. 相应的参数应具有兼容的类型。 If one type has a parameter type list and the other type is specified by a function declarator that is not part of a function definition and that contains an empty identifier list, the parameter list shall not have an ellipsis terminator and the type of each parameter shall be compatible with the type that results from the application of the default argument promotions .… [Emphasis added.] 如果一个类型具有参数类型列表而另一个类型由函数声明符指定,该函数声明符不是函数定义的一部分并且包含空标识符列表,则参数列表不应具有省略号终止符,并且每个参数的类型应为与应用默认参数促销产生的类型兼容 。... [强调添加。]

char is promoted to int , so char a is not compatible with an empty list. char被提升为int ,因此char a与空列表不兼容。 int a would be; int a将; int foo(); int foo(int a); is allowed. 被允许。

Essentially, the reason for this is that, if you declare a function int foo() and then call it, say with int foo(3) , the compiler has to know what to pass it. 本质上,原因是,如果你声明一个函数int foo()然后调用它,比如用int foo(3) ,编译器必须知道传递它的内容。 The rules, derived from history, are to perform the default argument promotions. 从历史派生的规则是执行默认参数提升。 So int foo(3) gets called as if it were int foo(int) . 所以int foo(3)被调用就像它是int foo(int)

Then, if you later define the function with int foo(char) , the definition will not match the call. 然后,如果稍后使用int foo(char)定义函数,则定义将与调用不匹配。 In many C implementations a char and an int may be put in the same place for calling a function, but a C implementation could do something different. 在许多C实现中, charint可以放在同一个地方用于调用函数,但是C实现可以做一些不同的事情。 A float and a double would be a greater conflict. floatdouble将是一个更大的冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不匹配的原型和定义与空参数列表在GCC和Clang中给出不同的结果? - Why does mismatched prototype and definition with empty argument list give different results in GCC and Clang? 为什么函数`memchr()` 使用`int` 作为`char` 类型的参数? - Why does the function `memchr()` use `int` for the argument of `char` type? 为什么在函数原型中命名参数是一个好习惯? - why it is a good practice to name the argument in the function prototype? 函数原型中的参数名称 - Argument name in function prototype 为什么此代码会发出警告:格式 '%s' 需要类型 'char *' 但参数 2 的类型为 'char(*)[11]'? - Why does this code give warning: format '%s' expects type 'char *' but argument 2 has type 'char(*)[11]'? 如果数组是 char *str[] 而不是 char *argv[] (主函数的参数之一),为什么 ++str 操作会导致错误? - Why ++str operation causes error if array is char *str[] but not in case of char *argv[] (one of the argument to the main function)? char argv[ ][ ] 作为主要函数参数,为什么不呢? - char argv[ ][ ] as main function argument, why not? char *作为C中函数的参数 - char* as an argument to a function in C strtok Function 带 char * 参数 - The strtok Function With char * Argument 参数与C中的原型不匹配 - Argument does not match prototype in C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM