Advanced printf() Usage

Through most of this chapter and the book, the printf() function has been used in a rather minimal way. We have used it to print simple messages or even those with variables, thanks to special placeholders. But the function can be customized in far more ways. Just one example is using %0.1f to format a float to a single decimal point (as in the temperature-conversion examples).

Each placeholder in a printf() statement can use many formatting parameters, each of which is placed between the initial percent sign and the final signifying character (c for character, s for string, d for integer, f for float, etc.). There are basically three extra formatting options.

First, there is a flag. This can be any one of the values in ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.