This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SPRINTF()

I am using SPRINTF() function and it increase the code by 2.3K. Is there any method to reduce the code size.

Thanks,

Mashood

  • sprintf() is a very comprehensive and powerful function and so it comes with a big memory footprint.

    What you could do is identify what part of the sprintf() functionality you actually need and "roll your own" string formatting function.

  • "sprintf() is a very comprehensive and powerful function"

    It is indeed!

    Just look it up in any 'C' textbook, and you'll that it takes several pages just to describe all the formatting options!
    If you think you could implement all that functionality in significantly less than 2.3K, there's nothing to stop you having a try...!

    Probably easier to just implement the bit you actually need, though... ;-)

    You could start by looking for open-source implementations of atoi, etc

  • Or perhaps start with an implementation of sprintf() and strip it down to just what you need.

    Source code for FreeBSD C library:

    www.freebsd.org/.../

    Many compilers come with several partial implementations of the printf()-style functions along with the full one. Many of those are even smart enough to snoop the format strings, detect which formatting options you actually use, and link the most minimal implementation which has all the features in use. Keil is not, alas, one of these (so far as I know!)

  • "Keil is not, alas, one of these (so far as I know!)"

    Alas, I think you're right there. :-(

  • Thanks you all, I got your point. Now my code size is slighly above 10K and I was using AT89C52 before and now I couldn't. So which controller from the same 51 family should i use. I have 89C55WD in hand with 20k program memory. Can it be used with the same code without any other settings in program or during program burn.

  • "Can it be used with the same code without any other settings"

    You would need to confirm that by comparing the two datasheets.