How to printf a length-delimited string

Thursday, November 15, 2012.

You often see code like this:

TODO insert example of copying length-delimited string to null-delimited string just for a printf

The extra copying isn’t necessary, since printf(3) can format length-delimited strings too.

I always end up looking this one up in the printf(3) man page.

 The precision

 An optional precision, in the form of a period ('.') followed by an optional decimal digit string. Instead of a decimal digit string one may write "*" or "*m$" (for some decimal integer m) to specify that the precision is given in the next argument, or in the m-th argument, respectively, which must be of type int. This gives ... the maximum number of characters to be printed from a string for s and S conversions.
Posted by Alan on Thursday, November 15, 2012. (Discuss)

blog comments powered by Disqus
maelstrom

"After a little while I became possessed with the keenest curiosity about the whirl itself. I positively felt a wish to explore its depths, even at the sacrifice I was going to make; and my principal grief was that I should never be able to tell my old companions on shore about the mysteries I should see."

Illustration for Edgar Allan Poe's story "Descent into the Maelstrom" by Harry Clarke, published in 1919.