Printf Optimization
All identifiers specified in the ANSI C standard library to have external linkage are always reserved for use as identifiers with external linkage. Therefore, compilers can optimize calls to library functions such as printf().
Example:
In the code fragment below, the routine printf() has external linkage, and therefore, is known to invoke the library function printf().
#include <stdio.h> void f (char *s) { printf ("%s", s); }
The format string can be parsed at compile time, and this particular call to printf() can be replaced with a call to fputs(), as shown below.
#include <stdio.h> void f (char *s) { fputs (s, stdout); }
Notes:
K&R C did not reserve the identifiers of library routines with external linkage, and this specification of ANSI C and its implications are not understood by all programmers and compiler developers, thus causing some confusion regarding the optimization of printf() and other library functions.
Parsing printf() format strings at compile time and replacing with calls to other library functions can significantly improve the run-time performance of some applications and benchmarks.
In addition to improving run-time performance, parsing printf() format strings can also be used to isolate program errors by matching the format string with the types and number of arguments in the argument list.