sprintf format issue?

Code snippet:

  ...
  char date_buff[40];
  DateTime n = rtc.now();
  float far = dht.readTemperature(true); 
  sprintf(date_buff, "%2d:%02d %2d/%2d/%2d %f", n.hour(),n.minute(),n.month(),n.day(),n.year()-2000), far;
  Serial.println(date_buff);
  ...

/* 
    Where:
       n.hour() = 7
       n.minute = 20
       n.month() = 2
       n.day() = 8
       n.year() = 2018
       far = 79.89
*/

Displays on Serial Monitor as:

7:20 2/8/18 ?

Why does the float value of 79.89 display a question mark?

If I change the float format to "%f.1" it displays:

7:20 2/8/18 ?.1

The sprintf() function as provided by the Arduino IDE does not support the formatting of floats using the %f parameter. There are ways round this if you search the forum.

Float data types are not supported in sprintf in Arduino. One way around it is to use dtostrf to convert the float to a string and use %s in the format specifier.

Bij default, float isn't implemented on Arduino because of the large code size.

Most of the time it's a lot more effective to not use sprint(f)() in the first place :wink:

Even more effective is not to use floats at all. It is not necessary for temperature measurements. Scale everthing to 1/100th degrees and store it in an int

Agreed!