But I can't wrap my head around what the snprintf( ) command is/does. I don't understand what it is that you don't understand... The xxprintf(xxstuff, "format string", arg1, arg2, ...) functions all print the contents of the format string, with a variable number of arguments that are translated … Answer from westfw on forum.arduino.cc
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Snprintf clarification - General Guidance - Arduino Forum
October 8, 2025 - I'll preface this by saying I'm a mechanical engineer who's been asked to build a device and also look at the programming, the mechanical bit is easy, I'm struggling a little with some of the programming; I'm using a Nano R4 and LoRa wan shield to send some data to a "things" server, I've pulled ...
How to use snprintf ? Dec 4, 2017
8y ago
sprintf ? Oct 6, 2012
13y ago
Using the sprintf function Jun 3, 2014
12y ago
snprintf - Deutsch Nov 20, 2019
6y ago
More results from arduino.cc
🌐
Reddit
reddit.com › r/arduino › snprintf works with integers but not with floats.
r/arduino on Reddit: snprintf works with integers but not with floats.
January 7, 2024 -

I am getting a bit confused and I need some help.The following code works fine:

    const uint8_t MESSAGE_MAX_SIZE = 10;
    char message[MESSAGE_MAX_SIZE];
    uint8_t val ;
    val = 99;
    (void)snprintf(message, MESSAGE_MAX_SIZE,"%d", val);
    Serial.println(message);

whereas the following does not:

    const uint8_t MESSAGE_MAX_SIZE = 10;    
    char message[MESSAGE_MAX_SIZE];
    float val ;
    val = 1.23;
    (void)snprintf(message, MESSAGE_MAX_SIZE,"%f", val);
    Serial.println(message);

Firstly, I get the following warning:

(32 of 107): warning: format '%f' expects argument of type 'double', but argument 4 has type 'float' [-Wformat=]

and what I get from the serial is the character ?.

If I accommodate the warning with e.g.:

(void)snprintf(message, MESSAGE_MAX_SIZE,"%f", (double)val);

then I get the same result, i.e. ? from the serial.

Discussions

Useless messages using < sprintf() and snprintf()>
Hi all, I don't know if this is related to Arduino IDE or not... However, when I use < snprintf() > in Arduino IDE 2.3.4 and run 'Verify' (Ctrl + R), I get these two messages: More on github.com
🌐 github.com
5
December 11, 2024
arduino - Custom snprintf() function gives wrong output - Stack Overflow
Background I'm trying to write a wrapper function for snrpintf() on an embedded platform (Arduino) to have a shorthand for writing into a buffer then printing it over the Serial port. Problem As... More on stackoverflow.com
🌐 stackoverflow.com
c string - snprintf doesn't display float values - Arduino Stack Exchange
I don't understand why the formatted string is displaying ?? instead of float values. My code: #include #include #include "SparkFunCCS811.h" #include More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
how to snprintf to serial with String object type ?
Hello, I would like to run the following simple code to simulate my problem: typedef struct { String sensorName; byte pin; byte state; } sensorData; // sensors on this node {sensorname,pinnr,begin_state} sensorData sensorTable[6] = { {"POG04",1,0}, {"POG14",1,0}, {"ROG04",1,0}, {"ROG14",1,0}, ... More on forum.arduino.cc
🌐 forum.arduino.cc
2
0
September 18, 2011
🌐
Arduino Forum
forum.arduino.cc › projects › programming
snprintf() - stuck - Programming - Arduino Forum
March 11, 2018 - Folks, I AM trying, but find it frustrating when I search and get a "File not found" when I click on a link; snprintf() search on google I am really not getting how things are explained in this case and many others. S…
🌐
Programming Electronics Academy
programmingelectronics.com › home › sprintf() with arduino | print multiple variables to the serial monitor
sprintf() with Arduino | Print multiple variables to the serial monitor
November 13, 2023 - Sprintf() with Arduino cannot handle floating point values. So if you have to print something that has a decimal point, like 3.14 or 156.7, then you need to convert that float value to a character string first, and then print the string.
🌐
Zbotic
zbotic.in › home › arduino string formatting: sprintf, dtostrf and print tips
Arduino String Formatting: sprintf, dtostrf and Print Tips - Zbotic
May 20, 2026 - Regular sprintf() has a critical vulnerability: if your format string produces more characters than the buffer can hold, it writes past the end of the buffer — a classic buffer overflow. On Arduino, this can corrupt the stack, cause seemingly random crashes, or corrupt other variables. snprintf(buffer, maxLen, format, args...) adds a maximum length parameter:
🌐
E-tinkers
e-tinkers.com › 2020 › 01 › do-you-know-arduino-sprintf-and-floating-point
Do you know Arduino? – sprintf and floating point – E-Tinkers
T> void print(const char *str, T... args) { int len = snprintf(NULL, 0, str, args...); if (len) { char buff[len+1]; snprintf(buff, len+1, str, args...); Serial.print(buff); } } Now I have a one liner print() function that is elegant and easy to read: print("temperature = %.1fc, Humidity = %.1f%%\n", temp, humidity); After done all those research and implement my own print function, I'm wondering how other Arduino-compatible platforms handle floating point in their Arduino core implementation?
Find elsewhere
🌐
GitHub
github.com › espressif › arduino-esp32 › issues › 10716
Useless messages using < sprintf() and snprintf()> · Issue #10716 · espressif/arduino-esp32
December 11, 2024 - void setup() { Serial.begin(9600); // const uint8_t kBffrSize8 = 6; uint16_t decVal16 = 3; // any value between 0 and 4095 MAX.(0xFFF) char ary_hexNUM[kBffrSize8]; // snprintf(ary_hexNUM, kBffrSize8, "0xX", decVal16); // Serial.print(F("ary_hexNUM[-3-] ==> ")); for (uint8_t i = 0; i < 6; i++) Serial.print(ary_hexNUM[i]); Serial.println(); // } // setup() void loop() { // } // loop()
Author   espressif
🌐
Vilimpoc
vilimpoc.org › blog › 2015 › 11 › 18 › sprintf-snprintf-problem-on-arduino
sprintf / snprintf Problem on Arduino.
/** * Demonstrate that the sprintf / snprintf implementation on Arduino is broken. * * sprintf / snprintf, when used with more than 8 varargs: * inserts an extra '00' in the output string * removes the last parameter from the output string * * This causes a lot of extra debugging.
🌐
Wolles Elektronikkiste
wolles-elektronikkiste.de › startseite › formatted output
Formatted output • Wolles Elektronikkiste
September 2, 2024 - For formatted output on the serial monitor or on displays, you can use the convenient functions sprintf() and snprintf().
🌐
C++ for Arduino
cpp4arduino.com › 2020 › 02 › 07 › how-to-format-strings-without-the-string-class.html
How to format strings without the String class | C++ for Arduino
February 7, 2020 - Before we say goodbye, I’ll like to present my all-time favorite function of the whole Arduino ecosystem. It’s snprintf_P(), but I call it “the holy grail” because it’s a hidden treasure that does everything one would want. It is identical to snprintf() except that it reads the format string from the Flash memory, and therefore reduces the RAM consumption.
🌐
Stack Exchange
arduino.stackexchange.com › questions › 80401 › snprintf-doesnt-display-float-values
c string - snprintf doesn't display float values - Arduino Stack Exchange
#include <Arduino.h> #include <Wire.h> #include "SparkFunCCS811.h" #include <DFRobot_SHT3x.h> DFRobot_SHT3x sht3x(&Wire, /*address=*/0x44, /*RST=*/4); //* Get SHT31 readings void readSHT(float &temp, float &humid) { DFRobot_SHT3x::sRHAndTemp_t data = sht3x.readTemperatureAndHumidity(sht3x.eRepeatability_High); char shtVal[50]; if (data.ERR == 0) { temp = data.TemperatureC; humid = data.Humidity; Serial.println(data.TemperatureC); Serial.println(data.Humidity); } snprintf(shtVal, 50, "Temp = %f Humidity = %f", temp, humid); Serial.println(shtVal); } void setup() { Serial.begin(115200); //*Initi
🌐
Arduino Forum
forum.arduino.cc › projects › programming
how to snprintf to serial with String object type ? - Programming - Arduino Forum
September 18, 2011 - Hello, I would like to run the following simple code to simulate my problem: typedef struct { String sensorName; byte pin; byte state; } sensorData; // sensors on this node {sensorname,pinnr,begin_state} sensorData sensorTable[6] = { {"POG04",1,0}, {"POG14",1,0}, {"ROG04",1,0}, {"ROG14",1,0}, {"LOG04",1,0}, {"TOG04",1,0} }; void setup() { Serial.begin(57600);}; int debug =1; // function to write data to Log void writeLog(char* msg) { if (debug == 1) {Serial.println(msg);...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
[SOLVED] Problem with snprintf and signed and unsigned long - Programming - Arduino Forum
January 27, 2014 - Hi I want to use signed long and unsigned long with snprintf. after some reading up I think I should use the ul and il tag. However that does not give me the wanted result. For instance: void setup() { // put your setup code here, to run once: delay(1000); } void loop() { // put your main code here, to run repeatedly: static unsigned long smallUnsigned =1; static unsigned long bigUnsigned =100000; static unsigned long smallSigned =-100000; static unsigned long bigSigned =1000...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Snprint With String Values - Programming - Arduino Forum
December 26, 2021 - Hello, I am trying to use the newly released Microsoft Azure IOT library. There is a line in the initial sketch which I have modified heavily that states. // replace the following line with your data sent to Azure IoTHub snprintf(buff, 128, "{\"topic\":\"iot\"}"); I have tried to replace this with snprintf(buff, 128, "{\"" + MQTTTopic + "\":\"" + MQTTPayload + "\"}"); Which is erroring on compilation. error: cannot convert 'StringSumHelper' to 'const char*' for argument '3' to...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Printf on Arduino - #6 by david_2018 - Programming - Arduino Forum
July 24, 2021 - printf is not functionally implemented in the standard Arduino environment, see https://playground.arduino.cc/Main/Printf/ You can use sprintf() or snprintf() to put the text into a char array, then print the array.
🌐
Mbed OS
forums.mbed.com › application development
Sprintf with "X" does not left pad with "0" - Application development - Arm Mbed OS support forum
June 16, 2020 - I’m porting code from an Arduino application. The following code produces the correct char array in Arduino: char hexval[5]; int gatetime= 124; sprintf(&hexval[0], “X”, gatetime); This produces 07C which is what…