I want to know if there is a way to write like in C programming:

  printf("the num is %d",num);  

I know that to print a line or a variable the command will be Serial.println when I write:

 Serial.println("the num is %d",num);   

It's not working.

Is there a way to write it in just one line?

Serial.print("the num is "); Serial.println(num);

is how you do it with the Serial object. There is a printf in the library and it's a lot easier to use (as you already know) but a little bit more work to set up. Here is a simple program using printf:

// Define a file descriptor for the serial output:
static FILE uartout = { 0 };

// Declare our put-character function:
static int uart_putchar (char c, FILE *stream);

void setup() {
   // Fill in UART file descriptor with pointer to my char-out func.
   fdev_setup_stream(&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
   stdout = &uartout;

   Serial.begin(9600);   // open serial port, sets baud rate

void loop( void ){
   printf("Run time: %ld\n", millis());
   delay(1000);          // wait 1 sec

// My char output function
static int uart_putchar (char c, FILE *stream)
   if( c == '\n' )
   Serial.write(c) ;
   return 0 ;
  • That's a nasty way to print a single text line. – adrianTNT Nov 9 '20 at 17:00
  • 1
    For just one line? Of course it is! – JRobert Nov 9 '20 at 19:24
  • right; for some reason I really didn't consider it could be useful when called many times – adrianTNT Nov 10 '20 at 14:26

I use:

Serial.println("The temperature is " + String(temperature) + " degrees C");

With this solution you can't format the variable's value but I find this quick and dirty for debug messages.

  • 1
    I use it too and i think it's the easiest way. – George Sep 13 '15 at 21:29

You can try "extending" the Print class to include a printf() function. It requires modification of the Arduino core to add it, and it's something I have been meaning to suggest to the Arduino guys as a permanent modification, but I often manually add a function such as this:

void Print::printf(const char *fmt, ...) {
    va_list va;
    va_start(va, fmt);
    char temp[1];
    uint32_t slen = vsnprintf(temp, 1, fmt, va);
    char out[slen + 2];
    va_start(va, fmt);
    vsnprintf(out, slen+1, fmt, va);

to my core.

It's not wonderfully efficient from a processing point of view - it first runs a vsnprintf() to a single byte target. This allows it to work out how long the formatted string will need to be:

If the output was truncated due to this limit, then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available.

So it then knows how much memory is needed to store the formatted string. It then goes ahead and allocates that much memory on the stack and re-formats the string again into that newly allocated memory. It can then go ahead and send that data to the serial port.

It's a "quick and dirty" hack, but it works. A better solution would be to re-write the printf() parser in the function and instead of formatting to memory then printing, to format the data stream directly to the Serial device, thus saving the double-processing and memory allocation.

You can then quite happily do things like:

Serial.printf("The temperature is %d degrees C\n", temperature);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.