What is RONG that I am doing with sprintf() function?

I have wished to convert a float number (say: 23.7) into a string using NANO/UNO and sprintf() function; but, I am not getting the correct values. The Serial.println(myData[0], HEX); shows 0x3F instead of 0x32. Would appreciate to have my mistake/ignorance being pointed.

sketch:

char myData[5];

void setup()
{
  Serial.begin(9600);
  float myTemp = 23.7;
  sprintf(myData, "%.1f", myTemp);
  Serial.println(myData[0], HEX);    // shows: 3F instead of 32
}

void loop(){}
1 Like

On which arduino?

Arduino NANO/UNO.

1 Like

It’s not implemented. (For Floating point types). Try on an ESP32 it will work
(Or use dtostrf( ) avr-libc: <stdlib.h>: General utilities)

1 Like

It's an AVR run-time library thing, not an Arduino thing.

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#gaa3b98c0d17b35642c0f3e4649092b9f1

If the full functionality including the floating point conversions is required, the following [linker] options should be used:

-Wl,-u,[vfprintf] -lprintf_flt -lm

It works well with both ESP8266 NodeMCU and DUE.

Thanks for the information which I know now.

Where to put in the UNO code? Any example?

it's not in the UNO code, you would need to modify the compilation (linker step) flags used by the IDE. this is not a simple preference to change in the IDE... (I think you need to explore creating a compiler.cpp.extra_flags property in a local file like platform.local.txt... Not sure about the details, read about this some time ago)

1 Like

found this:

Just choose it from the menu:

Oh yeah, you need to add this to boards.local.txt first:

menu.printf=AVR printf Version
menu.scanf=AVR scanf Version

uno.printf=default
uno.menu.printf.default=Default printf
uno.menu.printf.default.avr_printf_flags=
uno.menu.printf.full=Full printf
uno.menu.printf.full.avr_printf_flags=-Wl,-u,vfprintf -lprintf_flt
uno.menu.printf.minimal=Minimal printf
uno.menu.printf.minimal.avr_printf_flags=-Wl,-u,vfprintf -lprintf_min
uno.scanf=default
uno.menu.scanf.default=Default scanf
uno.menu.scanf.default.avr_scanf_flags=
uno.menu.scanf.full=Full scanf
uno.menu.scanf.full.avr_scanf_flags=-Wl,-u,vfscanf -lscanf_flt
uno.menu.scanf.minimal=Minimal scanf
uno.menu.scanf.minimal.avr_scanf_flags=-Wl,-u,vfscanf -lscanf_min
uno.compiler.c.elf.extra_flags={avr_printf_flags} {avr_scanf_flags}

nano.printf=default
nano.menu.printf.default=Default printf
nano.menu.printf.default.avr_printf_flags=
nano.menu.printf.full=Full printf
nano.menu.printf.full.avr_printf_flags=-Wl,-u,vfprintf -lprintf_flt
nano.menu.printf.minimal=Minimal printf
nano.menu.printf.minimal.avr_printf_flags=-Wl,-u,vfprintf -lprintf_min
nano.scanf=default
nano.menu.scanf.default=Default scanf
nano.menu.scanf.default.avr_scanf_flags=
nano.menu.scanf.full=Full scanf
nano.menu.scanf.full.avr_scanf_flags=-Wl,-u,vfscanf -lscanf_flt
nano.menu.scanf.minimal=Minimal scanf
nano.menu.scanf.minimal.avr_scanf_flags=-Wl,-u,vfscanf -lscanf_min
nano.compiler.c.elf.extra_flags={avr_printf_flags} {avr_scanf_flags}

That's for Uno and Nano, you have to add similar lines for ech board you want to use.

1 Like

cool stuff :slight_smile:

Where should I look for that file in my PC -- I mean the location path? I have made a search in my PC; it is apparently not found!

I guess that you need to create it if it does not exists. Search for boards.txt and pick the directory that is the most recent. Either in C:\Program Files (x86)\Arduino\hardware\arduino\avr if you never updated the AVR boards package or in a subdirectory of C:\Users\YourUsername\AppData\Local\Arduino15\packages\arduino\hardware\avr\version if you did update it.

1 Like

There is also the dtostrf() function for converting a float to a char array.
You can also print the float directly, if you do not need the string elsewhere.

Unfortunately, the file does not exit in my PC. I have looked in both the paths you have referred

You missed the below from my original reply (emphasis added).

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.