C++ 当使用浮点时,snprintf返回一个包含问号的字符串

C++ 当使用浮点时,snprintf返回一个包含问号的字符串,c++,floating-point,arduino,printf,C++,Floating Point,Arduino,Printf,我在arduino中使用snprintf将浮点打印到字符*。由于一些错误,我目前在读取实际值时遇到问题,但这不是这里的实际问题。我得到的字符串只包含“?”。我想知道这是NaN还是INF 例如: char temp[100]; float f; //This float is initialised on some value, but i'm currently not really sure what this value is, but for example 1.23 f = 1.23 s

我在arduino中使用snprintf将浮点打印到字符*。由于一些错误,我目前在读取实际值时遇到问题,但这不是这里的实际问题。我得到的字符串只包含“?”。我想知道这是NaN还是INF

例如:

char temp[100];
float f; //This float is initialised on some value, but i'm currently not really sure what this value is, but for example 1.23
f = 1.23
snprintf(temp, 100, "%f", f);

temp现在只包含“?”

考虑到您的意思是

char temp[100];
float f;    
f = 1.23;
snprintf(temp, 100, "%f", f);

它按预期工作。

Arduino的snprintf实现没有浮点支持。必须改用dtostf()

因此,不要这样做:

char temp[100];
float f;    
f = 1.23;
snprintf(temp, 100, "%f", f);
使用Arduino,我必须做到:

char temp[100];
float f;    
f = 1.23;
dtostrf(f , 2, 2, temp); //first 2 is the width including the . (1.) and the 2nd 2 is the precision (.23)

这些家伙在avrfreaks论坛上发现了这一点:

你还没有将f初始化为任何值。我故意忽略了这一点,因为我不确定在
arduino printf float
上的floatQuick search中有什么值似乎表明printf函数族默认情况下可能不支持float。。。我不相信。我更感兴趣的是你是如何使用
temp
作为C字符串的。事实并非如此,这个字符串包含arduino上的“?”。嗯。。。我用g++在Windows和Ubuntu上测试了它,效果很好。Arduino不使用g++也不使用标准的gcc库。因为它忽略了海报的上下文,所以被降级了。上面的代码适用于任何完整的实现-但是arduino作为一个小平台是不完整的,并且没有实现%f-这让大多数人第一次想到这一点时感到惊讶。(让我惊讶)如果你错过了我的评论,你也可以启用对它的支持,如果你看。