C语言 C中的函数调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14036894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:51:37  来源:igfitidea点击:

function call in C

cfunction

提问by nzomkxia

Possible Duplicate:
Why does gcc allow arguments to be passed to a function defined to be with no arguments?

可能的重复:
为什么 gcc 允许将参数传递给定义为没有参数的函数?

the code:

编码:

#include <stdio.h>
#include <stdlib.h>

void test_print()
{
    printf("test print\n");
}

int main()
{
    test_print(1,2);
    return 0;
}

although the caller of test_print in main has different amount of arguments with the defination of this function, the code can work very well, but if change it into c++ version, there occurs a compile error "too many arguments to funciton ....". why C allows argument mismatch call of function, when can we use this way of calling? and why it's forbidened in c++.

虽然main中test_print的调用者和这个函数的定义有不同数量的参数,代码可以很好地工作,但是如果把它改成c++版本,就会出现编译错误“too many arguments to funciton ....” . 为什么C允许参数不匹配的函数调用,我们什么时候可以使用这种调用方式?以及为什么它在 C++ 中被禁止。

System ubuntu 11.10
compiler: gcc 4.6.1

系统 ubuntu 11.10
编译器:gcc 4.6.1

回答by Alok Save

In c empty ()on an function means the function can take any number of arguments.
If you want to specify that function does not take any arguments you need to:

在 c 中(),函数为空意味着该函数可以接受任意数量的参数。
如果要指定该函数不带任何参数,则需要:

void test_print(void) 

While, in C++ empty ()on an function means it does not take any arguments. Note that this is significant in C++ because in C++ you can overload a function based on number of arguments it can take.

而在 C++ 中,空()函数意味着它不接受任何参数。请注意,这在 C++ 中很重要,因为在 C++ 中,您可以根据它可以采用的参数数量重载函数。

回答by Fingolfin

Because according to the C standard;

因为按照C标准;

int foo(void); //Is the only way a function won't allow any parameters

Quoting the standard:

引用标准:

10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

10 void 类型的未命名参数作为列表中唯一项的特殊情况指定该函数没有参数。

Leaving the parenthesis blank means non or any number of arguments.

将括号留空表示没有或任意数量的参数。

回答by Jonathan Leffler

When you write:

当你写:

void test_print() { ... }

you have not provided a prototype for the function, so the compiler is not supposed to compare calls with the argument list. To provide a prototype, you have to write an explicit void:

您没有为函数提供原型,因此编译器不应将调用与参数列表进行比较。要提供原型,您必须编写一个明确的void

void test_print(void) { ... }

Or provide a separate prototype declaration:

或者提供一个单独的原型声明:

void test_print(void);

But it is best to make the function definition match the prototype declaration, so always write the void. And yes, this is one of the areas where C++ differs from C. You can't use a function in C without a prototype in scope and C++ is able to treat an empty argument list as an empty argument list. In C99 or later, you're supposed to have a prototype in scope, but it isn't usually enforced by the compiler unless you add more stringent options (-Wmissing-prototypes -Wstrict-rprototypes -Wold-style-definition -Wold-style-declarationare possible GCC options). But the backwards compatibility requirements with pre-standard C meant that C89 could not enforce the 'empty parentheses means no arguments' rule without breaking a lot of previously valid C code, which would have prevented the standard from being acceptable.

但最好让函数定义与原型声明相匹配,所以总是写void. 是的,这是 C++ 与 C 不同的领域之一。你不能在没有范围原型的 C 中使用函数,C++ 能够将空参数列表视为空参数列表。在 C99 或更高版本中,您应该在范围内有一个原型,但编译器通常不会强制执行它,除非您添加更严格的选项(-Wmissing-prototypes -Wstrict-rprototypes -Wold-style-definition -Wold-style-declaration可能是 GCC 选项)。但是与前标准 C 的向后兼容性要求意味着 C89 无法在不破坏许多以前有效的 C 代码的情况下强制执行“空括号意味着没有参数”规则,这会阻止标准被接受。

回答by Avio

It is a good programming discipline to ensure that you find the compiler option that ensures that all functions are declared with a full prototype before being used, and to ensure that you use the compiler option all the time, and heed its warnings

确保您找到确保所有函数在使用前都使用完整原型声明的编译器选项,并确保您始终使用编译器选项并注意其警告,这是一个很好的编程纪律