I notice that lots of arduino fans are not coming from a C programming background. Even the C++ people may not know such a neat C feature, the sprintf function. I use it so often in my codes and libraries thus it demands a small introduction.

The following is a link to the sprintf function (I’ll be adding this link to my sample codes):

Essentially the sprintf function takes in a few parameters, such as integers, floats etc. and then follow a format string, output a formatted string that involves the parameters. Since arduino doesn’t print float (to reduce compiled file size), you may need to print two integers for a float. Say if I want to print on LCD the current time in the following format:


I have three variables:

int hour=13;
 int min=2;
 int sec=0;

If I do the simple print, I have to do the following:


And the result is less than perfect:

So how do I demand the formatting of the numbers to have a maximal 2 integer digits and no missing leading zeros? I have to tell the print function so by means of a format string. Let’s start from simple and add all details step-by-step:

A format string is just a string.
First I want to output integer (not float or else), so integer output is “%d”, or “%ld” for long integer.
Next I want 2 digit integer so “%2d”, or “%4d” if I wanted 4 digit integer.
Next I want to keep leading zero so my time looks right, so”%02d”. If I used “%2d” then leading spaces are provided so I get 13:(space)2:(space)0

Now I’m done with format string, it’s time to call the sprintf:

char[9] buffer=""; ///< This is the buffer for the string the sprintf outputs to
sprintf(buffer, "%02d:%02d:%02d", hour, min, sec); ///< This has 3 2-digit integers with leading zeros, separated by ":" . The list of parameters, hour, min, sec, provides the numbers the sprintf prints out with.
lcd.print(buffer); ///< You will get "13:02:00"

This will get you:


From the above example you can see that you can mix regular texts such as “:” or else with the format string in a complex output. Say we want to output a count-down number like “You have XX minutes left.”, then we do:

sprintf(buffer, "You have %2d minutes left.", min);

You always need to have a buffer char array long enough to hold the output. Count the characters before you define the length of the buffer array and add 1 to the count for string terminator. For the above string, the output is 25 characters, the “%2d” counts as 2 characters. Then you do:

char [26] buffer="";

It’s worth mentioning that sprintf is just for the look. There is no reason to use it if you don’t care about the look of your output.

Now that you have read so much about sprintf, you may also want to know sscanf, which is the opposite. It picks out number from a complex string so if you have a string like “13:02:00”, you can extract hour, min, and sec from the string. This is especially useful when accepting parameters from serial port. If you want to know about this function, read here or reply to this post saying you are interested in a post about the sscanf.


%d bloggers like this: