What does printf() output do in an XC8 program?

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:

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.