Arduino的:printf的/打印fprintf中的问号,而不是浮动 [英] Arduino: printf/fprintf prints question mark instead of float

查看:882
本文介绍了Arduino的:printf的/打印fprintf中的问号,而不是浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code为一个Arduino素描:

I have the following code for an Arduino sketch:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
static FILE lcdout = {0} ;

static int lcd_putchar(char ch, FILE* stream)
{
    lcd.write(ch) ;
    return (0) ;
}

void setup() {
  lcd.begin(16, 2);
  fdev_setup_stream (&lcdout, lcd_putchar, NULL, _FDEV_SETUP_WRITE);
}

void loop() 
{
  stdout = &lcdout;
  printf("%.2f Volts", 2.0);
}

问题出现在code的最后一行。这应该打印出2.00伏特,但相反,它打印?伏(一个问号,而不是实际的浮点数)。如果我试图格式化一个整数,这个伟大工程。

The problem comes at the last line of the code. This should print out "2.00 Volts" but instead, it prints "? Volts" (a question mark instead of the actual float value). If I try to format an integer, this works great.

所以基本上,如果我用下面的替换printf的线,它会正常工作:

So basically, if I replace the printf line with the following, it will work properly:

printf("%d Volts", 2); //prints correctly "2 Volts"

任何想法有什么问题呢?

Any idea what's the problem ?

推荐答案

GNU工具为AVR单片机(这是包含在Arduino的IDE)默认使用C标准库的精缩版,在其中,例如,浮点支持,从格式化的I / O功能降低/拿走了(只是为了的printf()以适应几个千字节芯片的长期储存。)

The GNU toolchain for AVRs (which is included with the Arduino IDE) uses a "minified" version of the C standard library by default, in which, for example, the floating-point support is reduced/taken away from formatted I/O functions (just in order printf() to fit in the few kBytes long storage of the chip.)

如果你想要这个工作,你有agains包含的普通版本的printf(),使用 -Wl另一个库链接,-u,vfprintf -lprintf_flt 连接器选项。

If you want this to work, you have to link agains another library containing the normal version of printf(), by using the -Wl,-u,vfprintf -lprintf_flt linker flags.

这篇关于Arduino的:printf的/打印fprintf中的问号,而不是浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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