Arduino Basics: sprintf function

8 May 2019

sprintf function

Original tutorial at: https://arduinobasics.blogspot.com/2019/05/sprintf-function.html
 
 

Description

This tutorial will help you to understand the sprintf function, and how to use it. Essentially, the sprintf function allows you to construct a string using a pre-formatted string template to which you can insert variables at pre-defined locations. The sprintf function will "compile" the string and assign it to a char array. All you have to do is make sure that the char array is large enough to hold all of the characters in the string. The best way to understand the sprintf function is with examples. And luckily, I have examples. What are we waiting for ? Let's dive in.

Parts Required: an Arduino or ESP32 and a USB cable.

 
 

sprintf ( char* array,   const char* strTemplate,   var1...);

 
 

Arduino IDE

While there are many Arduino IDE alternatives out there, I would recommend that you use the official Arduino IDE for this project. I used the official Arduino IDE app (v1.8.5) for Windows 10.
Make sure to get the most up-to-date version for your operating system here.


 
 

Arduino Code

The code below will show you how to use the sprintf function and includes a number of different format specifiers to play with. In each case the sprintf function writes to the "data" character array, and subsequently sends it through to the Serial monitor. The string template helps to construct the data output, allowing you to insert variables at specific locations within the text. The format of the variable is defined by the "format specifier" used in the sprintf function. The format specifier is always prefixed with a percentage sign (%). Please note that some of the format specifiers may not work with your specific MCU. I seemed to get this to work fine on an ESP32, but on an Arduino, the float, scientific and big number sections were a bit hit and miss.

 
 

Serial Monitor Output

  1. Upload the code to the Arduino.
  2. Open the Serial monitor in the Arduino IDE (Ctrl+Shift+M).
  3. Ensure that you have set the baud rate in the Serial monitor to 9600.
  4. You should see the following output:

 

The sprintf function requires that you have a character array to store the output. In the example code above, the output is stored in the "data" character array. It also requires a template that tells the function where to insert the variables. As you can see from the table below, the variables will be formatted based on the format specifier used. The format specifier can be quite useful for numeric conversions. Eg. decimal to hex conversions.

 
 

Format Specifiers

Some of the different format specifiers that can be used with the sprinf function are listed below.

Conclusion

Now that you know all about the sprintf function, I hope it will inspire you to use it in your own projects. Please let me know in the comments below how you use the sprintf function, and whether there was anything that you feel I failed to mention in this tutorial.
Happy Coding !!

 
 

If you found this tutorial helpful, please consider supporting me by buying me a virtual coffee/beer.

$3.00 AUD only
 

Social Media

You can find me on various social networks:

Follow me on Twitter: ScottC @ArduinoBasics.
I can also be found on Instagram, Pinterest, and YouTube.
And if all else fails, I have a server on Discord.


11 comments:

  1. Shame the tutorial isn't correct! The format specifiers above are not all implemented in the arduino library and you should not expect these to work.

    ReplyDelete
    Replies
    1. Works fine on an ESP32. I just re-tried it with an UNO, and it seems that some format specifiers are not implemented. Just try it and see what works for you on your MCU.

      Delete
    2. Not all available on Mega 2560 either

      Delete
  2. Hi and thanks for the nice description.

    But what if I want 6 fixed width characters with decimal:
    " 12.23" or "123.67" or "-34.67"
    3 digit and 2 decimal
    %.2f give only "12.23"
    trying %3.2f doesn't work. neither trying %6.2f

    ReplyDelete
    Replies
    1. Do you want leading zeros ? If so then maybe %06.2f
      Haven't tried it, but maybe give that a go.
      I think it counts the decimal point as a character - so you may need to adjust to suit.

      Delete
  3. Oups
    Not sure it doesn't work !
    I migh have another error in my code

    ReplyDelete
  4. Hello, I enjoyed your tutorial and I may confirm that sprintf with floats and chars is working ok into my ESP8266 application.
    Thanks

    ReplyDelete
  5. I copied and pasted the code. Doesn't work with arduino Nano.

    ReplyDelete
    Replies
    1. some format specifiers will not work on a nano - but some will..

      Delete
  6. hi,
    please update your tuto with unsigned formating (it is also usefull)

    data[100];
    uint16_t uval = 0 ;
    uval -=1 ;
    sprintf(data,"%u",uval ); // "65535"

    uint32_t ulval = 0 ;
    ulval -=1 ;
    sprintf(data,"%lu",ulval ); // "4294967295"

    thank you

    ReplyDelete

Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked on my Arduino Tutorials Discord Server.

Comments are moderated due to large amount of spam.