"..." 是什么意思? c中的函数声明是什么意思?

发布于 2024-12-15 11:17:13 字数 205 浏览 4 评论 0 原文

我正在查看 MinGW 附带的 stdio.h 头文件,注意到 printf 函数是这样声明的:

int printf (const char *__format, ...)
{
    //body omitted
}

我以前从未在函数参数列表中见过省略号,所以我尝试了一下。它编译并运行没有错误。那么,“……”的目的是什么?

I was going through the stdio.h header file that comes with MinGW and noticed that the printf function is declared like this:

int printf (const char *__format, ...)
{
    //body omitted
}

I have never seen ellipsis in function parameter list before so I tried it out. It compiles and runs without error. What, then, is the purpose of "..."?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伊面 2024-12-22 11:17:15

这意味着该函数是一个可变参数函数,它采用可变数量的参数:

http://en.wikipedia.org/wiki/Variadic_function

printf() 本身可能是可变参数函数的最佳示例。

That means that the function is a variadic function that takes a variable number of parameters:

http://en.wikipedia.org/wiki/Variadic_function

printf() itself is probably the best example of a variadic function.

梦里泪两行 2024-12-22 11:17:15

它通知编译器该函数有一个可变参数列表。该功能仅适用于 __cdecl 调用约定。它允许调用者在最后一个固定参数之后指定所需的任何参数值,因为调用者将在函数退出时清理参数。可变参数通常用于 printf 风格的函数,其中可变参数值的解释取决于固定参数值的值(例如将各个可变参数与 __format 中的每个格式说明符相匹配)范围)。

It informs the compiler that the function has a variadic list of parameters. It is a feature that only works with the __cdecl calling convention. It allows the caller to specify whatever parameter values it wants after the last fixed parameter, as the caller will clean up the parameters when the function exits. Variadic parameters are commonly used for printf-style functions where the interpretation of the variadic parameter values is dependant on the value of the fixed parameter values (such as matching up individual variadic parameters to each format specifier in a __format parameter).

阳光的暖冬 2024-12-22 11:17:15

它用于允许可变数量的参数或未指定类型的参数,如 printf() 所做的那样。允许可变数量参数的函数称为 可变参数函数

可变参数变量通过 va_start 访问va_listva_endva_arg

可变数量的参数 (...)

示例实现:

#include <stdarg.h>

double average(int count, ...)
{
    va_list ap;
    int j;
    double tot = 0;
    va_start(ap, count); //Requires the last fixed parameter (to get the address)
    for(j=0; j<count; j++)
        tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument.
    va_end(ap);
    return tot/count;
}

希望这会有所帮助。

it is used to allow a variable number of arguments or parameters of unspecified type, as printf() do. the function which allows variable number of argumnets is called Variadic Function

Variadic Variables are accessed with va_start, va_list, va_end and va_arg

Variable number of Arguments (...)

Sample Implementation:

#include <stdarg.h>

double average(int count, ...)
{
    va_list ap;
    int j;
    double tot = 0;
    va_start(ap, count); //Requires the last fixed parameter (to get the address)
    for(j=0; j<count; j++)
        tot+=va_arg(ap, double); //Requires the type to cast to. Increments ap to the next argument.
    va_end(ap);
    return tot/count;
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文