在 C 中调用参数较少的函数的后果? [英] consequences of calling a function with fewer arguments in C?

查看:21
本文介绍了在 C 中调用参数较少的函数的后果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数,它接受一些参数和一个指针参数.调用函数时,有时我需要传递在函数内部使用的指针,有时则不需要.调用参数较少的函数会产生什么后果?它编译正确并且在运行时它仍然很好,但这是好的编程吗?如果我用虚拟变量调用函数会更好吗?感谢和抱歉初学者的问题.

I wrote a function that that takes some argument and a pointer argument . when calling the function , sometimes I need to pass along the pointer for use inside the function and sometimes I don't . what are the consequences of calling a function with fewer arguments ? it compiles correctly and during runtime its still fine , but is this good programming ? is it better if I call the function with a dummy variable ? Thanks and sorry for beginner question .

推荐答案

如果你调用一个参数太少的函数并且编译器没有抱怨,那么你做错了.

If you call a function with too few arguments and the compiler doesn't complain, then you're doing something wrong.

可以编写一个函数声明/定义,而不指定它需要多少个参数:

You can write a function declaration/definition that doesn't specify how many arguments it requires:

void func();
/* ... */
func();
func(arg1);
func(arg1, arg2);

这三个调用都会被编译器接受,但至少有两个是不正确的.

All three of those calls will be accepted by the compiler, but at least two of them are incorrect.

自 1989 年 ANSI C 标准以来,这种形式的函数声明/定义已经过时.

That form of function declaration/definition has been obsolescent since the 1989 ANSI C standard.

切勿使用此表格.

函数声明应该总是写成原型,即指定参数数量和类型的声明.作为一种特殊情况,(void) 表示一个没有参数的函数.

Functions declaration should always be written as prototypes, i.e., declarations that specify the number and type(s) of the parameters. As a special case, (void) denotes a function with no parameters.

void func(int arg);
/* ... */
func();           /* rejected by compiler */
func(arg1);       /* accepted -- but only if arg1 is of type int or convertible to int */
func(arg1, arg2); /* rejected by compiler */

如果您设法编写的代码调用具有错误数量的参数的函数并使其通过编译器,则行为未定义.它可能看起来有效",但当您使用不同的编译器或相同的编译器和不同的选项编译它时,它可能会在您面前炸毁.

If you manage to write code that calls a function with an incorrect number of arguments and get it past the compiler, the behavior is undefined. It might appear to "work", but it could blow up in your face when, for example, you compile it with a different compiler, or with the same compiler and different options.

一个复杂因素:一些函数是可变参数,采用可变数量的参数.最常见的例子是printf.对于可变参数函数,所需的参数通常由函数的文档指定——并且正确获取参数同样重要.不同之处在于,对于可变参数函数,您的编译器不一定会告诉您调用不正确.

One complication: some functions are variadic, taking a variable number of arguments. The most common example of this is printf. For variadic functions, the required arguments are typically specified by the function's documentation -- and it's just as important to get the arguments right. The difference is that, for variadic functions, your compiler won't necessarily tell you that a call is incorrect.

, ... 语法(在函数声明中)和 中定义的宏是唯一的编写和使用采用可变数量和类型参数的 C 函数的合法方式.

The , ... syntax (in the function declaration) and the macros defined in <stdarg.h> are the only legitimate way to write and use C functions that take a variable number and type(s) of arguments.

这篇关于在 C 中调用参数较少的函数的后果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆