c语言函数中调用的参数太多

您所在的位置:网站首页 输入的函数参数太少 c语言函数中调用的参数太多

c语言函数中调用的参数太多

2024-07-10 13:57| 来源: 网络整理| 查看: 265

c语言函数中调用的参数太多

很少参数无法使用C语言 (Too few arguments to function in C language)

This error occurs when numbers of actual and formal arguments are different in the program.

当程序中实际参数和形式参数的数量不同时,会发生此错误。

Let’s understand first, what actual and formal arguments are?

首先让我们了解一下什么是实际和形式上的争论

Actual arguments are the variables, values which are being passed while calling a function and formal arguments are the temporary variables which we declare while defining a function.

实际参数是变量,调用函数时传递的值,形式参数是我们在定义函数时声明的临时变量。

Consider the given example:

考虑给定的示例:

int sum(int a, int b, int c) { return (a+b+c); } int main() { int x,y,z; x = 10; y = 20; z = 30; printf("sum = %d\n",sum(x, y, z)); return 0; }

Here, actual arguments are x, y and z. Formal arguments are a, b and c.

在这里,实际参数是x , y和z 。 形式参数是a , b和c 。

When, the error "too few arguments to function is occurred"?

什么时候出现错误“函数的参数太少”

Remember: Number of actual arguments (the arguments which are going to be supplied while calling the function) must equal to number of formal arguments (the arguments which are declared while defining a function).

切记:实际参数(调用函数时将要提供的参数)的数量必须等于形式参数(在定义函数时声明的参数)的数量。

This error will occur if the number of actual and formal arguments is different.

如果实际参数和形式参数的数量不同,则会发生此错误。

Consider the above example, and match with these below given calling statements, these statements will generate errors:

考虑上面的示例,并与以下给定的调用语句匹配,这些语句将生成错误:

printf("sum = %d\n", sum(x, y)); printf("sum = %d\n", sum(x)); printf("sum = %d\n", sum(10, 20));

Example: (by calling functions with less number of arguments)

示例:(通过调用较少参数的函数)

#include int sum(int a, int b, int c) { return (a+b+c); } int main() { int x,y,z; x = 10; y = 20; z = 30; printf("sum = %d\n", sum(x, y)); printf("sum = %d\n", sum(x)); printf("sum = %d\n", sum(10, 20)); return 0; }

Output

输出量

prog.c: In function ‘main’: prog.c:12:23: error: too few arguments to function ‘sum’ printf("sum = %d\n", sum(x, y)); ^~~ prog.c:3:5: note: declared here int sum(int a, int b, int c) ^~~ prog.c:13:23: error: too few arguments to function ‘sum’ printf("sum = %d\n", sum(x)); ^~~ prog.c:3:5: note: declared here int sum(int a, int b, int c) ^~~ prog.c:14:23: error: too few arguments to function ‘sum’ printf("sum = %d\n", sum(10, 20)); ^~~ prog.c:3:5: note: declared here int sum(int a, int b, int c) ^~~ prog.c:9:10: warning: variable ‘z’ set but not used [-Wunused-but-set-variable] int x,y,z; ^

So, while calling a function, you must check the total number of arguments. So that you can save your time by this type of errors.

因此,在调用函数时,必须检查参数总数。 这样您就可以节省此类错误的时间。

Example: (by calling functions with correct number of arguments)

示例:(通过使用正确数量的参数调用函数)

#include int sum(int a, int b, int c) { return (a+b+c); } int main() { int x,y,z; x = 10; y = 20; z = 30; printf("sum = %d\n", sum(x, y, z)); printf("sum = %d\n", sum(x,y,z)); printf("sum = %d\n", sum(10, 20,30)); return 0; }

Output

输出量

sum = 60 sum = 60 sum = 60

翻译自: https://www.includehelp.com/c/too-few-arguments-to-function-c-language-error.aspx

c语言函数中调用的参数太多



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3