{"id":314,"date":"2013-10-05T00:01:35","date_gmt":"2013-10-05T08:01:35","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=314"},"modified":"2013-10-12T07:37:15","modified_gmt":"2013-10-12T15:37:15","slug":"conversion-character-mania-text-output","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=314","title":{"rendered":"Conversion Character Mania: Text Output"},"content":{"rendered":"<p>The <em>printf()<\/em> function&#8217;s conversion characters <code>%s<\/code> and <code>%c<\/code> seem to be rather straightforward. Or are they?<br \/>\n<!--more--><br \/>\nFirst the easy one: To output a single character of text, use <code>%c<\/code>. That conversion character has no options, settings, or optional detritus that can be wedged between the <code>%<\/code> and the <code>c<\/code>. Simple.<\/p>\n<p>The <code>%s<\/code> conversion character displays strings of text. The entire string is displayed, from the start to the character before the terminating null, character <code>\\0<\/code>. That&#8217;s how <code>%s<\/code> is used most often, but it&#8217;s not the limits of how you can format a string of text for output.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[] = \"spatula\";\r\n\r\n    printf(\"%s\\n\",string);\r\n    printf(\"%12s\\n\",string);\r\n    printf(\"%-12s\\n\",string);\r\n    printf(\"%12.3s\\n\",string);\r\n    printf(\"%-12.3s\\n\",string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>If you&#8217;ve messed with the <code>%s<\/code> conversion character (as shown in my books), then you probably have used some of the examples shown in the above code. Most frequently, <code>%12s<\/code> and <code>%-12s<\/code>. Both limit output to a span of 12 characters; the <code>-<\/code> sign left-justifies the output. These are the same width and alignment options available for formatting numeric output.<\/p>\n<p>What may seem odd to you is the <code>.3<\/code> option. That&#8217;s the precision setting, which has a unique effect on strings. Here&#8217;s the program&#8217;s output:<\/p>\n<p><code><\/p>\n<pre>spatula\r\n     spatula\r\nspatula     \r\n         spa\r\nspa        <\/pre>\n<p><\/code><\/p>\n<p>In the first line <code>%s<\/code> outputs the entire string.<\/p>\n<p>The second uses <code>%12s<\/code> to right-justify the output within a width of 12 characters. As with numeric output, if the string is longer than 12 characters, the entire string is displayed; no justification.<\/p>\n<p>The third example uses <code>%-12s<\/code> to left-justify output within a width of 12 characters. When the string is longer than the width value, no justification is used.<\/p>\n<p>The fourth and fifth examples add a period and a second value, <code>.3<\/code>. The effect this &#8220;precision&#8221; value is to truncate the entire string to the number of characters specified.<\/p>\n<p>So the conversion character <code>%12.3s<\/code> sets output to a 12 character width and truncates output to only the first three characters of the string. The output is right-justified:<\/p>\n<p><code><\/p>\n<pre>         spa<\/pre>\n<p><\/code><\/p>\n<p>The conversion character <code>%-12.3s<\/code> sets output to a 12 character width, left-justified, and truncates the string to the first 3 characters:<\/p>\n<p><code><\/p>\n<pre>spa<\/pre>\n<p><\/code><\/p>\n<p>The truncating doesn&#8217;t affect the string itself, only the output.<\/p>\n<p>Unlike numeric conversion characters, the <code>+<\/code> and <code>0<\/code> prefix before the width value aren&#8217;t allowed for <code>%s<\/code>. If you want that sort of output, you&#8217;ll have to code it yourself.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=327\">Next lesson<\/a> I wrap up my Conversion Character Mania series with the rest of the lot not yet covered.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Conversion character choices for displaying characters and strings of text are, thankfully, not that complex. Well, that is unless you want them to be. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=314\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-314","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=314"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/314\/revisions"}],"predecessor-version":[{"id":359,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/314\/revisions\/359"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}