HOWTO · Arduino
Arduino printf Function
Learn why printf() is not universal in Arduino and compare sprintf() with Serial.print() for formatted serial output, including float and buffer limits.
On this page
The standard Arduino programming environment does not provide a portable printf() call that writes to the serial monitor. Some board cores add their own Serial.printf() method, so check the documentation for your selected board. For portable sketches, format into a character buffer with sprintf() and send it with Serial.print() or Serial.println(). If you do not need a format string, several serial calls can build the same message directly.
Does Arduino Provide printf()?
There is no single printf() interface guaranteed across Arduino boards. The C library’s printf() normally writes to a standard output stream, which a microcontroller environment may not connect to a serial port. A core-specific Serial.printf() can exist, but its availability and supported format specifiers depend on the target. The sketches below use setup() and loop() and write through Serial; a small host-side serial stand-in lets the same examples run without an Arduino board. On a board, the Arduino core supplies Serial and the sketch lifecycle.
Format Text with sprintf() and Send It over Serial
Use sprintf() when a message needs placeholders or padding. It writes the completed text into a character array; Serial.println() then sends that text and a line ending. This sketch uses snprintf() to pass the array capacity to the formatter and checks that the complete message fits before sending it:
#include <cstdio>
#ifdef ARDUINO
#include <Arduino.h>
#else
class HostSerial {
public:
void begin(unsigned long) {}
void println(const char* text) { std::puts(text); }
};
HostSerial Serial;
#endif
void setup() {
Serial.begin(9600);
char buffer[32];
const int value = 42;
const int length = std::snprintf(buffer, sizeof(buffer), "The answer is: %d", value);
if (length >= 0 && static_cast<unsigned int>(length) < sizeof(buffer)) {
Serial.println(buffer);
}
}
void loop() {}
#ifndef ARDUINO
int main() {
setup();
return 0;
}
#endif
The answer is: 42
snprintf() returns the number of characters it would write, excluding the terminating null character. If that count is at least the array capacity, the sketch does not send a partial message. Check that the selected board’s C library provides snprintf() as expected; the availability and behavior of formatted functions can vary by board and core.
Build the Output with Serial.print()
For simple labels and values, direct calls avoid a temporary formatted buffer. Serial.print() continues on the same line; Serial.println() ends it. This sketch assembles the same line with serial calls:
#include <cstdio>
#ifdef ARDUINO
#include <Arduino.h>
#else
class HostSerial {
public:
void begin(unsigned long) {}
void print(const char* text) { std::printf("%s", text); }
void print(int value) { std::printf("%d", value); }
void print(double value, int digits) { std::printf("%.*f", digits, value); }
void println(const char* text) { std::printf("%s\n", text); }
};
HostSerial Serial;
#endif
void setup() {
Serial.begin(9600);
const int temperature = 25;
const double humidity = 60.5;
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C, Humidity: ");
Serial.print(humidity, 1);
Serial.println("%");
}
void loop() {}
#ifndef ARDUINO
int main() {
setup();
return 0;
}
#endif
Temperature: 25 C, Humidity: 60.5%
The host-side stand-in models the calls used here so the sketch’s line can be shown without hardware; it is not an Arduino core. On the board, Serial.print(humidity, 1) requests one digit after the decimal point, and the exact supported formatting depends on the board’s Serial implementation.
Handle Floating-Point Values and Buffer Limits
Floating-point formatting through sprintf() is not consistent across Arduino cores: some embedded C libraries omit float conversion from formatted I/O to save program memory. Verify the target core before relying on %f. The direct Serial.print(value, digits) form is an alternative when the core supports the usual Arduino Print implementation.
Any formatted output must fit in its character array, including the terminating \0. Use snprintf() with the array’s actual capacity where available, and check its return value before using the result. If the formatted text is longer than the array, enlarge the buffer or shorten the message. A larger buffer consumes more RAM, which is limited on many boards.
Choose the Right Approach
Choose sprintf() or snprintf() when a single format string makes a structured message easier to read, and send the result with Serial.println(). Choose Serial.print() calls when you only need to join a few labels and values. Both approaches send text through the serial interface; neither guarantees identical formatting support on every board. Confirm core-specific methods and float behavior against the board you compile for.