The printf() function performs two main tasks: formatting of text and printing this formatted text to stdout. The exact location of stdout is determined by a second function called putch(), which is called by printf() to output each character.
The printf() function performs the formatting and then calls a helper function, called putch, to send each byte of the formatted text. By default the putch() function is empty. It should be customized to suit the project at hand. By customizing the putch function, you can have printf send data to any peripheral or location. The printf() function is used to print to the Universal Synchronous Asynchronous Receiver Transmitter (USART), but it could define stdout to output to an LCD screen or Serial Peripheral Interface (SPI). The code to initialize the intended destination must be executed before printf() is called.
The sample code for putch configured for USART transmission on a PIC16 device:
The sample code for putch configured for USART transmission on a PIC18 device: