是否有需要浮不加倍printf的说明? [英] Is there a printf specifier that requires float not double?
问题描述
我得到MISRA型错误,当我使用的snprintf
%F说明符类型的参数浮动
。
I'm getting MISRA type errors when I use "%f" specifier for snprintf
with a parameter of type float
.
根据我的研究,MISRA是正确的,因为%Fexpectes一种双
。
According to my research, MISRA is correct because "%f" expectes a type of double
.
是否有浮点说明或修改,将使用浮动
类型参数,而不是一个双击
?
Is there a floating point specifier or modifier that will use a float
type parameter and not a double
?
我工作在嵌入式系统上,不希望从32位浮点转换为64位双击
只是为了取悦的snprintf
功能。在code打印到调试/控制台端口而这正是发生转换的地方。
I'm working on an embedded system and don't want to convert from 32-bit float to 64-bit double
just to please the snprintf
function. The code prints to the debug / console port and this is the only place where the conversion takes place.
对于那些你需要一个code例如:
For those of you needing a code example:
// This section is for those C++ purists and it fulfills the C++ tag.
#if __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif
#define BUFFER_SIZE (128U)
int main(void)
{
char buffer[BUFFER_SIZE];
float my_float = 1.234F;
// The compiler will promote the single precision "my_float"
// to double precision before passing to snprintf.
(void)snprintf(buffer, BUFFER_SIZE, "%10.4f", my_float);
puts(buffer);
return 0;
}
我的,因此所有的研究和网络是关于打印浮点值,不是这说明符将需要一个浮动
参数,以便没有晋升到双
发生。
All my research on SO and the Web is about printing a floating point value, not about which specifiers will require a float
parameter so that no promotion to double
takes place.
我使用ARM7TDMI处理器的IAR Embedded Workbench编译器。
I am using IAR Embedded Workbench compiler for ARM7TDMI processor.
推荐答案
没有,因为的printf
和它的朋友是可变参数的函数,所以浮动
参数经过自动转换为双击
作为的默认参数促销活动的一部分的(见C99标准第6.5.2.2 )。
No, because printf
and its friends are variadic functions, so a float
parameter undergoes automatic conversion to double
as part of the default argument promotions (see section 6.5.2.2 of the C99 standard).
我不知道为什么,这虽然会触发一个警告MISRA,我想不出任何办法中,这可能是危险的。
I'm not sure why this triggers a MISRA warning though, I can't think of any way in which this might be dangerous.
这篇关于是否有需要浮不加倍printf的说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!