{"id":2972,"date":"2018-02-24T00:01:44","date_gmt":"2018-02-24T08:01:44","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2972"},"modified":"2018-02-17T10:05:57","modified_gmt":"2018-02-17T18:05:57","slug":"variable-width-values-in-conversion-characters","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2972","title":{"rendered":"Variable Width Values in Conversion Characters"},"content":{"rendered":"<p>The topic of conversion characters, or <em>printf()<\/em> percent placeholders, is vast and confusing. Most programmers know the basics, but the details are so complex that some handy shortcuts are overlooked. One of those shortcuts is the <code>*<\/code> wildcard that sets width values.<br \/>\n<!--more--><br \/>\nThe tables and examples of conversion characters found in the K&#038;R book are dry and lack examples. In my books, I try to spice them up, but I must be choosy because too much detail makes the reader&#8217;s eyes glaze over. With so many options, variations, and choices, it would consume too many pages to document everything. Without a zippy plot, no one would read it.<\/p>\n<p>Here on the C Blog, I&#8217;ve reviewed various placeholder options in my <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=288\">Conversion Character series<\/a>. My approach is by variable type as opposed to the placeholder letters. So I didn&#8217;t even mention the <code>*<\/code> option. In the K&#038;R book, the paragraph after the conversion character table goes:<\/p>\n<blockquote><p>A width or precision may be specified as *, in which case the value is computed by converting the next argument (which must be an int).<\/p><\/blockquote>\n<p>An example is provided, but &mdash; seriously &mdash; at that point in your C education, you&#8217;re so overwhelmed that the text merrily slips into the ether. Yet, the <code>*<\/code> width specifier is useful and kind of fun.<\/p>\n<p>As an example, the <code>%f<\/code> placeholder converts <em>float<\/em> values and variables. To set precision, you use a width format such as <code>%.2f<\/code>, which outputs only two digits after the decimal. If you instead use <code>%.*f<\/code>, you can add an <em>int<\/em> argument to the <em>printf()<\/em> function to set the precision at runtime:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=1;x&lt;=10;x++)\r\n        printf(\"%.*f\\n\",x,M_PI);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In the above code, the value of &pi; (defined as <code>M_PI<\/code> in the <code>math.h<\/code> header file) is displayed in degrees of precision from 1 to 10. The placeholder is <code>%.*f<\/code>. Variable <code>x<\/code> in the <em>printf()<\/em> statement sets the width. Here&#8217;s the output:<\/p>\n<pre><code>3.1\r\n3.14\r\n3.142\r\n3.1416\r\n3.14159\r\n3.141593\r\n3.1415927\r\n3.14159265\r\n3.141592654\r\n3.1415926536<\/code><\/pre>\n<p>For other placeholders that specify width, you sandwich the <code>*<\/code> between the <code>%<\/code> and the conversion character:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=6;x&lt;=16;x++)\r\n        printf(\"%*s\\n\",x,\"Hello!\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The above code uses the <code>%*s<\/code> placeholder to set a variable width for string output. Strings are right-aligned by default, so the effect is that the string moves to the right within a variable-width space, as the output shows:<\/p>\n<pre><code>Hello!\r\n Hello!\r\n  Hello!\r\n   Hello!\r\n    Hello!\r\n     Hello!\r\n      Hello!\r\n       Hello!\r\n        Hello!\r\n         Hello!\r\n          Hello!<\/code><\/pre>\n<p>Upon reviewing my own code, I see that I&#8217;ve used the <em>sprintf()<\/em> function instead of the <code>*<\/code> character; <em>sprintf()<\/em> created a formatting string for <em>printf()<\/em>, one that used variable width. Using the <code>*<\/code> instead would have been better. I shall remember to do so in the future!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yet another obscure C language fact, but one that proves powerful when you remember to use it. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2972\">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-2972","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\/2972","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=2972"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2972\/revisions"}],"predecessor-version":[{"id":2985,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2972\/revisions\/2985"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}