可变参数函数(va_arg)不适用于浮点数? [英] Variadic function (va_arg) doesn't work with float?

查看:21
本文介绍了可变参数函数(va_arg)不适用于浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有浮点参数的可变参数函数.为什么它不起作用?

va_arg(arg, float)

解决方案

对应于 ... 的函数的参数在传递给可变参数函数之前提升.charshort 提升为 intfloat 提升为 double 等.

<块引用>

6.5.2.2.7 函数原型声明器中的省略号会导致参数类型转换在最后一个声明的参数之后停止.默认参数提升是在尾随参数上执行的.

原因是早期版本的 C 没有函数原型;参数类型在函数站点声明,但在调用站点不知道.但是不同类型的表示方式不同,传递参数的表示方式必须符合被调用函数的期望.因此 char 和 short 值可以传递给带有 int 参数的函数,或者可以将 float 值传递给带有 double 参数的函数,编译器将较小的类型提升"为较大的类型.当在调用站点不知道参数的类型时,仍然会看到这种行为——即,对于可变参数函数或没有原型声明的函数(例如,int foo();).p>

I have a variadic function which takes a float parameter. Why doesn't it work?

va_arg(arg, float)

解决方案

Parameters of functions that correspond to ... are promoted before passing to your variadic function. char and short are promoted to int, float is promoted to double, etc.

6.5.2.2.7 The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.

The reason for this is that early versions of C did not have function prototypes; parameter types were declared at the function site but were not known at the call site. But different types are represented differently, and the representation of the passed argument must match the called function's expectation. So that char and short values could be passed to functions with int parameters, or float values could be passed to functions with double parameters, the compiler "promoted" the smaller types to be of the larger type. This behavior is still seen when the type of the parameter is not known at the call site -- namely, for variadic functions or functions declared without a prototype (e.g., int foo();).

这篇关于可变参数函数(va_arg)不适用于浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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