{"id":3518,"date":"2019-03-09T00:01:30","date_gmt":"2019-03-09T08:01:30","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3518"},"modified":"2019-03-02T10:28:13","modified_gmt":"2019-03-02T18:28:13","slug":"building-a-format-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3518","title":{"rendered":"Building a Format String"},"content":{"rendered":"<p>In the <em>printf()<\/em> function, the first argument is a format string. It can contain the various percent placeholders to represent values expressed in the remaining arguments. But can one of those arguments also contain placeholders?<br \/>\n<!--more--><br \/>\nThe answer is &#8220;No.&#8221; Only the first argument is scanned for the percent placeholders. So the following trick doesn&#8217;t work:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"My name is %s\\n\",\"Dan %s\",\"Gookin\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>I suppose the logic here is that the second argument, <code>\"Dan %s\"<\/code>, is somehow combined with the format string to allow access to the third argument. When you compile the code, however, you receive a warning about a surplus argument in the <em>printf()<\/em> statement. The output looks like this:<\/p>\n<p><code>My name is Dan %s<\/code><\/p>\n<p>Because the first argument to the <em>printf()<\/em> statement can be a variable, it&#8217;s possible to construct the format string on-the-fly to include however many arguments you desire. Such a maneuver is tricky and you must be careful to ensure that the number of placeholders matches the number of arguments lest the program go haywire.<\/p>\n<p>To make it work, employ the string-building function, <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=391\"><em>sprintf()<\/em><\/a>. Concoct the string so that it contains the proper number of placeholders, as is done in the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char format0[32];\r\n    char format1[] = \"My name is %s\";\r\n    char format2[] = \" %s\\n\";\r\n\r\n    sprintf(format0,\"%s%s\",format1,format2);\r\n    printf(format0,\"Dan\",\"Gookin\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Three character buffers are used in this code: <code>format0[]<\/code>, <code>format1[]<\/code>, and <code>format2[]<\/code>. The first is storage and the second two are formatting strings.<\/p>\n<p>Line 9 uses the <em>sprintf()<\/em> function to concatenate <code>format1[]<\/code> and <code>format[2]<\/code>, placing the result into the <code>format0[]<\/code> buffer. The result is a formatting string with two <code>%s<\/code> placeholders. These come from the separate strings. (The <code>\"%s%s\"<\/code> formatting string in the <em>sprintf()<\/em> function is what concatenates the two strings.) Here&#8217;s the resulting format string:<\/p>\n<p><code>\"My name is %s %s\\n\"<\/code><\/p>\n<p>This string is used in the <em>printf()<\/em> statement at Line 10, which then gobbles arguments <code>\"Dan\"<\/code> and <code>\"Gookin\"<\/code> to create the output:<\/p>\n<p><code>My name is Dan Gookin<\/code><\/p>\n<p>I suppose the point of this Lesson isn&#8217;t so much that you cannot create a formatting string by using other arguments in the <em>printf()<\/em> function, but that a method to accomplish what you need is probably available otherwise. In this example, creating a flexible formatting string is possible, but the entire string must be created first, not within the <em>printf()<\/em> function itself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first argument of the <em>printf()<\/em> function can be a variable, but only when you craft it carefully. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3518\">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-3518","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\/3518","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=3518"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3518\/revisions"}],"predecessor-version":[{"id":3534,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3518\/revisions\/3534"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}