cancel
Showing results for 
Search instead for 
Did you mean: 

printf and sprintf why it should not be used?

orn
Associate II
Posted on November 17, 2012 at 12:09

To print the result of the ADC on my LCD (16x2) I use the following actions:

sprintf(Sprint,''%d'',ADC3ConvertedValue[0]);

LCD_PrintData(Sprint, lenght(Sprint)); //  length (Sprint) returns the size of the carrier Sprint

I read on the internet that should be avoided for microcontrollers the instructions sprintf and printf, but I have not found an explanation as to because I avoid them! Can someone clarify because I should avoid use printf and sprintf?

What are the ''codes'' alternative to these two functions?

#know-your-tools
3 REPLIES 3
Posted on November 17, 2012 at 13:10

printf and scanf have quite a large footprint in memory, and on the stack, especially when float support is also pulled in. It's also a general solution, where some more efficient and compact solutions might be more desirable. Embedded can be intolerant of many of these issues.

Converting numerics is mostly an exercise is modulo math.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/F0 Discovery - fprintf function implementation&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=132]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FF0%20Discovery%20-%20fprintf%20function%20implementation&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=132
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on November 17, 2012 at 14:37

As clive1 says, the big issue is the resource usage. This should come as no surprise: these are very powerful & versatile functions; their description run to several pages - so, obviously, it's going to take a lot of resources to implement all that power and versatility!

eg, a simple ''hello world'' program using printf can use up almost all of the allowed code space in the free Keil 8051 evaluation tools!

But chips like the STM32 are not nearly as tightly resource-constrained - so printf can be far less of an issue.

Also, many toolsets have options for a ''reduced'' printf implementation.

The answer, as always, is to Know Your Tools - know the cost of using prinf, and consider whether that is acceptable within your particular project requirements & constraints.
orn
Associate II
Posted on November 17, 2012 at 15:18

thanks for your suggestions, I have noticed that the functions printf and spintf are very ''slow'' and tried to figure out why, I'm trying to optimize my code, so I would like to create a function that converts the data acquired from 'ADC in characters, they can be sent to the LCD now I'm studying the codes that Clive has posted  :D