Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>printf()</code>, and a special format string:</p> <pre><code>char *str = "0123456789"; printf("%.6s\n", str + 1); </code></pre> <p>The precision in the <code>%s</code> conversion specifier specifies the maximum number of characters to print. You can use a variable to specify the precision at runtime as well:</p> <pre><code>int length = 6; char *str = "0123456789"; printf("%.*s\n", length, str + 1); </code></pre> <p>In this example, the * is used to indicate that the next argument (<code>length</code>) will contain the precision for the <code>%s</code> conversion, the corresponding argument must be an <code>int</code>.</p> <p>Pointer arithmetic can be used to specify the starting position as I did above.</p> <p><strong>[EDIT]</strong></p> <p>One more point, if your string is shorter than your precision specifier, less characters will be printed, for example:</p> <pre><code>int length = 10; char *str = "0123456789"; printf("%.*s\n", length, str + 5); </code></pre> <p>Will print "<code>56789</code>". If you always want to print a certain number of characters, specify both a minimum field width and a precision:</p> <pre><code>printf("%10.10s\n", str + 5); </code></pre> <p>or</p> <pre><code>printf("%*.*s\n", length, length, str + 5); </code></pre> <p>which will print:</p> <pre><code>" 56789" </code></pre> <p>You can use the minus sign to left-justify the output in the field:</p> <pre><code>printf("%-10.10s\n", str + 5); </code></pre> <p>Finally, the minimum field width and the precision can be different, i.e. </p> <pre><code>printf("%8.5s\n", str); </code></pre> <p>will print at most 5 characters right-justified in an 8 character field.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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