Over a million developers have joined DZone.

Why I don’t like printf()

DZone's Guide to

Why I don’t like printf()

· Big Data Zone ·
Free Resource

How to Simplify Apache Kafka. Get eBook.

How many times have I seen this:

printf("Hello world!\n");

I have a strong opinion, and a rule for using printf():

“Do *not* use printf()!”

Using printf(), as the other ‘related’ functions like scanf(), can be very hany: it is easy to write something to the console, or to build a string. I used this in “printf() with the FRDM-KL25Z Board and without Processor Expert” too ;-) . But in general it is bad. Really bad.

Code Size

Using printf() adds greatly to the code size of the application. I have seen cases where this is in the range of 10-20 KByte of code. The problem comes from the fact that printf(), as defined by the ANSI library standard, needs to support all the different format string. Including formatting octal numbers, floating point, etc. Even if you are not using octal numbers. Or when was the last time I used octal numbers? It is in there, and it adds up.

You might have a look at the printf() implementation of your library. If you have CodeWarrior, then have a look at


This is an implementation optimized for small memory systems, and tries hard to trim it down. The good thing is that you can disable a lot of stuff you do not need with this library implementation. That file is more than 1200 lines of source code just for printf().  Even with discounting the comments and empty lines, it is a *lot*. Yes, printf() does a lot of nice formatting things, but do I need it? Probably not. Do I need the fancy formatting strings I probably never will use? Nope. And one single usage of printf() can ruin my code size:

Printf Flash Size

Printf Flash Size

In this application, the usage of printf() vs. non-printf() functions has cut the code size by half. That 5K do not matter much if I have 1 MByte of FLASH. It matters if I have a smaller part say with 16 KByte or 32 KByte. Then using printf() is really a no-go.

But there are ways for improvement. For example this

printf("Hello world!\n");

is better written as

puts("Hello world!\n");

without the extra overhead.

:idea:  The Freescale HCS08 compiler even tries to replace calls to printf() with a call to puts() if enabled by an option. That’s cool, but better if the programmer is smarter.

RAM/Stack usage

The other negative impact of using printf(): it uses a lot of stack space. This because handling all the formatters plus the variable argument list has a price. It depends on the printf() implementation, but it easily adds 512-1024 bytes on the stack. If your application causes a stack overflow, it could be because of printf(). If you have multiple tasks in your system, then this easily adds up. Below is the same application from above, which shows the required RAM: using printf() has a severe impact too:

printf RAM usage

printf RAM usage

Again: if you have plenty of RAM available, wasting a few KBytes might not be a big deal. But it is again for a device with smaller amount of RAM. And engineers should not waste things ;-)


The other trap of printf() is: it is a constant source of programming errors.

For example what is wrong with

void printString(char *str) {


:idea:  There is of course one problem (inherent to the way C handles strings) that the string is not properly zero terminated: then it would print the bytes until the first zero byte, potentially the entire memory :-(

The problem was that my application crashed in a rally bad way. The problem was that the function received a string with ‘%’ in it:

printString("  %sssssssssss%\r\n);
printString("  %s  Value  s%\r\n);
printString("  %sssssssssss%\r\n);

The problem is that printf() will parse the ‘%’ and then will pop arguments from the stack which do not exist, causing a stack error. This easily leads to a system crash, or opens a program vulnerability (e.g. for hacker attack or security exploit).

The solution was to use this:

void printString(char *str) {
  printf("%s", str);

Additional special considerations and format string checking needs to be performed if the string can come from outside (e.g. user input), or gets constructed on the fly.

The GNU gcc has options as -Wall,-Wformat, -Wno-format-extra-args, -Wformat-security, -Wformat-nonliteral, but they only help at compile time, not for strings constructed at runtime.


printf() can cause a lot of troubles. It is adding to the code size, and it needs a lot of stack. Using printf() in ‘desktop’ applications or in applications where RAM/ROM footprint does not matter, is probably ok. But it is a security and stack overflow thread.

For smaller embedded systems where RAM/ROM footprint and stability is key, printf() and its variants should be banned from the application. Instead, safe string routines and simpler methods shall be used.

:idea:  I’m using the Utility module which has safe string manipulation routines: they all have the buffer size as an extra argument to avoid buffer overflow.

I agree that there is no need to re-invent the wheel, and printf() has a good side too. It is only that it should not be used in embedded system except for demonstration purposes ;-) .

Happy Printing

12 Best Practices for Modern Data Ingestion. Download White Paper.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}