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

查看:230
本文介绍了调用在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.

切勿使用这种形式。

函数声明应该<青霉>总是的被写成的原型的,即,指定的数量和参数的类型(多个)声明。作为一个特例,(无效)表示不带参数的函数。

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 */

如果你成功地写入code,随着参数数量不正确调用一个函数,并把它过去的编译器,行为是不确定的。它可能会出现工作,但它可以在你的脸上吹起来的时候,比如你用不同的编译器编译它,或者用相同的编译器和不同的选择。

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.

... 语法(在函数声明),并在中所定义宏; STDARG.H&GT; 的合法的方式编写和使用需要的参数变量数目和类型(S)的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天全站免登陆