<p>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 <a href="http://qt-project.org/doc/qt-4.8/qstring.html" rel="nofollow noreferrer">QString</a> 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 <strong>'printf primer'</strong> for Stackoverflow.</p>

<p>In the code, I came across this little gem,</p>

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

<p>How will the variables a, b, c, d, e, f, g be formatted?</p>

[{'answer_id': 7988, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 0, 'selected': False, 'text': '<p>a. decimal, four significant digits </p>\n\n<p>b. Not sure</p>\n\n<p>c. hex, minimum 4 characters </p>\n\n<p>d. Also hex, minimum 2 characters </p>\n\n<p>e. 1 character </p>\n\n<p>f. String of characters, minimum 4</p>\n'}, {'answer_id': 8035, 'author': 'Jason Day', 'author_id': 737, 'author_profile': 'https://Stackoverflow.com/users/737', 'pm_score': 4, 'selected': True, 'text': '<p>Danny is mostly right.</p>\n\n<p>a. unsigned decimal, minimum 4 characters, space padded<br>\nb. floating point, minimum 16 digits before the decimal (0 padded), 1 digit after the decimal<br>\nc. hex, minimum 4 characters, 0 padded, letters are printed in upper case<br>\nd. same as above, but minimum 2 characters<br>\ne. e is assumed to be an int, converted to an unsigned char and printed<br>\nf. same as e<br>\ng. 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.</p>\n\n<p>Edit: <a href="https://stackoverflow.com/users/430/jj33">jj33</a> points out 2 errors in b and g above <a href="https://stackoverflow.com/questions/7981/decoding-printf-statements-in-c-printf-primer#8051">here</a>.</p>\n'}, {'answer_id': 8051, 'author': 'jj33', 'author_id': 430, 'author_profile': 'https://Stackoverflow.com/users/430', 'pm_score': 3, 'selected': False, 'text': '<p>@<a href="https://stackoverflow.com/questions/7981/decoding-printf-statements-in-c-printf-primer#8035">Jason Day</a>, I think the 4 in the last %4s is significant if there are fewer than 4 characters.  If there are more than 4 you are right, %4s and %s would be the same, but with fewer than 4 chars in g %s would be left justified and %4s would be right-justified in a 4 char field.</p>\n\n<p>b is actually minimum 16 chars for the whole field, including the decimal and the single digit after the decimal I think (16 total chars vs 18 total chars)</p>\n'}, {'answer_id': 8075, 'author': 'Jason Day', 'author_id': 737, 'author_profile': 'https://Stackoverflow.com/users/737', 'pm_score': 2, 'selected': False, 'text': '<p>@jj33, you\'re absolutely right, on both counts.</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nint main(int argc, char *argv[]) {\n    char *s = "Hello, World";\n    char *s2 = "he";\n\n    printf("4s: \'%4s\'\\n", s);\n    printf(".4s: \'%.4s\'\\n", s);\n    printf("4s2: \'%4s\'\\n", s2);\n    printf(".4s2: \'%.4s\'\\n", s2);\n\n    return 0;\n}\n\n$ gcc -o foo foo.c\n$ ./foo\n4s: \'Hello, World\'\n.4s: \'Hell\'\n4s2: \'  he\'\n.4s2: \'he\'\n</code></pre>\n\n<p>Good catch!</p>\n'}, {'answer_id': 88898, 'author': 'pixelbeat', 'author_id': 4421, 'author_profile': 'https://Stackoverflow.com/users/4421', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s my printf primer:\n<a href="http://www.pixelbeat.org/programming/gcc/format_specs.html" rel="nofollow noreferrer">http://www.pixelbeat.org/programming/gcc/format_specs.html</a></p>\n\n<p>I always compile with -Wall with gcc which\nwill warn about any mismatches between the supplied\nprintf formats and variables.</p>\n'}, {'answer_id': 109077, 'author': 'Omer Zak', 'author_id': 11886, 'author_profile': 'https://Stackoverflow.com/users/11886', 'pm_score': 0, 'selected': False, 'text': '<p>What you really need is a tool which takes the format strings in printf() statements and converts them into equivalent QString based function calls.<br>\nDoes anyone want to spend his Free Software Donation Time on developing such a tool?</p>\n\n<p><em>Placeholder for URL to a Free Software hosting service holding the source code of such a tool</em></p>\n'}]
['https://Stackoverflow.com/questions/7981', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/25/']