Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The argument to the <code>*</code> before the <code>.</code> is the <em>field width</em> and the argument to the <code>*</code> after the <code>.</code> is the <em>precision</em>.</p> <p>Field widths are the minimum number of bytes that will be output as a result of the conversion; the output will be padded (by default, on the left with spaces, but left zero padding and right space padding are also options, controlled by flags) if fewer bytes would be produced. A negative argument to the <code>*</code> for width is interpreted as the corresponding positive value with the <code>-</code> flag, which moves the padding to the right (i.e. left-justifies the field).</p> <p><em>Precision</em> on the other hand has a meaning that varies according to the conversion being performed. Negative precisions are treated as if no precision had been specified at all. For integers, it's the minimum number of <em>digits</em> (not total output) to be produced; if fewer digits would be produced, zeros are added to the left. An explicit precision of 0 results in no digits being produced when the value is 0 (instead of a single <code>0</code> being produced). For strings, precision <em>limits</em> the number of output bytes, truncating the string (and permitting a longer, non-null-terminated, input array) if necessary. For floating point specifiers, precision controls the number of places printed, either after the radix point (for <code>%f</code>) or the total places of significance (for the other formats).</p> <p>In your examples:</p> <pre><code>printf("%*.*d\n", -6 , 7,20000); </code></pre> <p>Here the field is left-aligned (padding on right) with a minimum width of 6, but the field ends up being wider anyway, so the width is ignored. The precision of 7 forces integer output to be at least 7 digits, so you end up with <code>0020000</code> as the converted field contents, which already exceeded the width.</p> <p>In the other one:</p> <pre><code>printf("%*.*d\n", 5, -6, 2000); </code></pre> <p>The field width is 5, with the default right alignment; padding is with spaces on the left. The negative precision is ignored, as if it were not specified, so the converted field contents are <code>2000</code>, only 4 bytes, which get padded up to 5 bytes to fill the width by means of a single leading space.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload