Arduino:printf/fprintf 打印问号而不是浮点数 [英] Arduino: printf/fprintf prints question mark instead of float

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

问题描述

我有以下用于 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);
}

问题出在代码的最后一行.这应该打印出2.00 Volts",而是打印? Volts"(一个问号而不是实际的浮点值).如果我尝试格式化一个整数,这很好用.

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"

知道有什么问题吗?

推荐答案

AVR 的 GNU 工具链(包含在 Arduino IDE 中)默认使用 C 标准库的缩小"版本,例如,浮点支持从格式化的 I/O 函数中减少/取消(只是为了 printf() 适合芯片的几 KB 长存储.)

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.)

如果你想让它工作,你必须使用-Wl,-u,vfprintf -lprintf_flt 重新链接另一个包含普通版本的printf() 的库代码> 链接器标志.

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天全站免登陆