sprintf float output ?

Why float format here gives me "?" ?

float DV=0;
float BV=0;

int ma=0;
float mah=0;
float mahnow=0;
unsigned long timer= millis();
 if(timer-pstimer > 1000){
   sec++;
   
   float BV1 = analogRead(A2);
   BV = BV1/1023*5.18;  

   float DV1 = analogRead(A5);
   DV = DV1/1023*4.96;    
   
   ma=DV*1000;
   
   mahnow=DV/3.6;
   if (BV>0.1){
   mah=mah+mahnow;}
   
   lcd.setCursor(10,1);
   lcd.print("    ");

   pstimer = timer;
 }
char plistA[16];
sprintf(plistA, "%02d:%02d %6.1f mah",hr, minu, mah);
lcd.setCursor(0,0);
lcd.print( plistA );

char plistB[16];  
sprintf(plistB, "b:%3.2fv %04d  ma", BV, ma);
lcd.setCursor(0,1);
lcd.print( plistB );

Because %f is not supported by the Arduino environment in order to save space.

There are ways to re-enable the floating point support in printf, but it involves hacking around in the internals of your IDE installation, and makes anything that then uses printf much bigger.

Better is to either pre-format your floats into strings using dtostrf(), or to manually print the two parts (integer and decimal) of the float manually:

printf("%d.%04d", (int)floatValue, (int)(floatValue * 1000.0) % 1000);

thanks, that is logically.

majenko: There are ways to re-enable the floating point support in printf, but it involves hacking around in the internals of your IDE installation, and makes anything that then uses printf much bigger.

Better is to either pre-format your floats into strings using dtostrf(), or to manually print the two parts (integer and decimal) of the float manually:

printf("%d.%04d", (int)floatValue, (int)(floatValue * 1000.0) % 1000);

For those using an AVR board that want to know more about customizing the Arduino IDE and allowing the extra overhead of the full printf related tools, read on:

A solution can be found by reading the AVR documentation for the printf family of functions that will lead you to vfprintf… at the bottom there it describes compiler flags needed for properly linking in the full functionality (see following link):
https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#gaa3b98c0d17b35642c0f3e4649092b9f1

The problem is that knowing where to put the compiler flags can be difficult. Compiler options are found in a file called “platform.txt” and in this case we want to add to “compiler.c.elf.extra_flags”. If you want to add your own flags without messing up the default Arduino files then use “platform.local.txt” (you will have to create this file). Assuming Windows, for standard Arduino boards, the path to these files is similar to:
*C:\Program Files (x86)\Arduino\hardware\arduino\avr*

If you have a non-standard board, you will need to modify folder options to show hidden folders, and the path is similar to:
_C:\Users*\AppData\Local\Arduino15\packages*\hardware\avr**_

I would recommend simply adding the file “platform.local.txt” and in that file adding the following line:

compiler.c.elf.extra_flags=-Wl,-u,vfprintf -lprintf_flt -lm

That should do it. Floats will now be printed instead of “?”. :slight_smile:

NOTE: When enabling the full vfprintf library, my code memory footprint (flash) increased by 1472 bytes.