If you dont learn sprintf(), your code will hate you later

Printf stay tuned, subscribe to our youtube channel to get more videos like this. Are you learning arduino programming check out our membership program to learn the software and hardware skills youll need to build your own projects? You will get an all access pass to our high quality video training that covers everything from the basics of programming like variables and control structures up to using interrupts, arrays and more follow the link in the description to sign up today. All right lets say you want to print this line of text to the serial monitor where the number of burritos and the temperature value are both variables. Using the serial print function would take like five lines of code just to print out this single line of text. In fact, for every variable we add to the output string, we have to add two more serial prints in the code, so if we wanted to print something with four variables inserted into our string, itd take like nine lines of code. This is where s printf comes in handy. We can print out as many variables into our string as we want, and the amount of code required stays right at about three lines. Here are the three lines of code youll need. First, you need a character array to save the output string into then you need the s printf function, which is going to take some text and variables and combine it into a single string. Finally, well use the serial print function to display the formatted string, so lets take a closer look at each of these lines of code.

So the first thing is a character buffer character array. The character rate needs to be large or larger than the final output string. So you need to count the characters you plan to store in that string and make sure that the buffer is at least that large, the next line of code is the actual sprintf function. S printf stands for string print formatted. The function takes a minimum of two arguments. The first argument is where you plan to store the string that s printf will be making for you. This is where we use that character buffer that we just created on the previous line, so the string that s printf formats is going to be stored in this character buffer. The next argument is the string that you want to create its going to be filled in with format specifiers, where you want to insert your variables the format specifier starts with the percent sign in the letter following the percent sign is called the format character, and it Tells s printf what data type is going to be used for that variable. So in this example, we have two format specifiers. This means that we want two variables inserted into the output string. Now these character, specifiers are a little weird at first theyre. Just these letters that stand for the kind of data type thats going to be inserted, and once you learn what each letter means it starts to make a little more sense.

But until you figure that out its kind of like what does this mean, so here are some of the common character, specifiers a d or an. I is a signed decimal, integer, a? U is an unsigned decimal integer and an s is a string of characters. So here, when we see this percent sign d, we are telling s printf to format the inserted variable as a signed decimal integer. Now, if youre wondering like what the heck is a signed, decimal, integer, well, heres, a scoop signed, means that it can be positive or negative. Decimal means that we want it to show up in decimal form, instead of like formatted as an octyl or hexadecimal, or something like that. Integer means that its just a whole number, that is there arent any decimal points in it in this example, were also using the s character specifier. This specifies a string of characters, so where does s printf actually find the variables to insert? Well, we actually dont have to look too far, because those are the arguments added right after the string for every format. Specifier, you must pass a matching value. These values are added as additional arguments to s print f, each one separated by a comma. In this example, we have two format specifiers. Therefore, we have two arguments at the end. The first one num burritos will get inserted at that first format: specifier, the second one temp string will get inserted at the second format.

Specifier, if we add more format specifiers in our string, wed need to add more arguments to the end of s print f. Hopefully, this whole s, print f thing is kind of making sense so far, but maybe youve got this little nagging question right now. Youre, like hey, wait a second. I thought you said that the s character formatter was for a string of characters, but the temperature in fahrenheit is a floating point value. What gives well heres the deal s printf 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 you can print the string. A handy way to do. That is, with d to string f, which converts a floating point value to a string. We wont get into that now, but be sure to check out our other video on using d to string f with arduino, okay. The final line of code in our trifecta here is the good old serial print and what we pass as an argument is the character buffer. Where s printf stored, our formatted string, youll notice that s printf isnt returning the string itself. It saves that string into the character buffer we had specified, which is why all we have to do is print the buffers contents to display the string now its worth, noting that s printf does return a value if you choose to use it, which is the total Number of characters that have been stored into the buffer.

This return value excludes the null terminating character, thats, also added by s print f. So now, with these three lines of code, we can open up the serial, monitor and see that the string has been inserted with the variables showing up pretty nicely. So using just these three lines of code. We can insert a bunch of variables into a single string and print it out to the serial monitor and it comes up nicely formatted if we ever want to go back and add more variables to the string. All we have to do is add the appropriate format. Specifiers and then the corresponding values to the s printf function and hey were good to go. Okay lets do a quick review of what weve learned about s printf the first value that the sprintf function expects is a character buffer. This is where the formatted string will be stored. The second value in s, printf, is the string. You want to format with any format. Specifiers. The final arguments are the values you want to replace the format specifiers with now believe it or not, theres, actually a ton more stuff that you can do with s print f. In fact, between the percent sign and the character specifier, you can insert what are called sub specifiers, and these things will do everything from left justify the inserted values to add, leading zeros and more its actually pretty cool.

Share.
Exit mobile version