O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using sprintf

The printf function directs the result of any formatted input string combined with parameters to the screen. There is a companion function (sprintf) that performs the same formatting, but that directs the result to a string (character array). This enables the programmer to perform a variety of pre-formatting tasks.

Possible uses for this include creating record data in memory to be formatted for screen printing and file output within certain parameters at a later stage. It can also be useful in building up a line (or page) for output in one call rather than using multiple calls to printf, which can be quite slow.

The generic form for the sprintf function is as follows:

sprintf ( <output>, <format>, <variables . . .> )

In this definition, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required