Decoding printf statements in C (Printf Primer)

Tags: qstring printf qt c
Question!

I'm working on bringing some old code from 1998 up to the 21st century. One of the first steps in the process is converting the printf statements to QString variables. No matter how many times I look back at printf though, I always end up forgetting one thing or the other. So, for fun, let's decode it together, for ole' times sake and in the process create the first little 'printf primer' for Stackoverflow.

In the code, I came across this little gem,

printf("%4u\t%016.1f\t%04X\t%02X\t%1c\t%1c\t%4s", a, b, c, d, e, f, g);

How will the variables a, b, c, d, e, f, g be formatted?



Answers

What you really need is a tool which takes the format strings in printf() statements and converts them into equivalent QString based function calls.
Does anyone want to spend his Free Software Donation Time on developing such a tool?

Placeholder for URL to a Free Software hosting service holding the source code of such a tool

By : Omer Zak


Here's my printf primer: http://www.pixelbeat.org/programming/gcc/format_specs.html

I always compile with -Wall with gcc which will warn about any mismatches between the supplied printf formats and variables.

By : pixelbeat


Danny is mostly right.

a. unsigned decimal, minimum 4 characters, space padded
b. floating point, minimum 16 digits before the decimal (0 padded), 1 digit after the decimal
c. hex, minimum 4 characters, 0 padded, letters are printed in upper case
d. same as above, but minimum 2 characters
e. e is assumed to be an int, converted to an unsigned char and printed
f. same as e
g. This is likely a typo, the 4 has no effect. If it were "%.4s", then a maximum of 4 characters from the string would be printed. It is interesting to note that in this case, the string does not need to be null terminated.

Edit: jj33 points out 2 errors in b and g above here.

By : Jason Day


This video can help you solving your question :)
By: admin