1
void loop()
{

  float temperature = dht.readTemperature();
  char test[50];
  sprintf(test, "Temp: %f", temperature);

  Serial.println(temperature); // works fine
  Serial.println(test); // why it prints "?"
  
  delay(300);
}

// Output

24.10   <== Serial.println(temperature);
?       <== Serial.println(test);
24.10
?
24.10
?
24.10
?

Hi. I'm arduino/c newbie. Why print test is keep showing "?" Am I missing something? When I running the below code in c++ playground, it works fine.

float temperature = 23.19;
char test[50];
sprintf(test, "Temp: %f", temperature);
printf("%s", test);        <==== Works fine!
ton1
  • 7,238
  • 18
  • 71
  • 126
  • 2
    I think this article should help [sprintf in Arduino](https://stackoverflow.com/questions/27651012/arduino-sprintf-float-not-formatting) – marco Dec 31 '21 at 21:32
  • 1
    @RohanS Thanks a lot! I will close this article – ton1 Dec 31 '21 at 21:35

0 Answers0