{"id":391,"date":"2013-11-16T00:01:34","date_gmt":"2013-11-16T08:01:34","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=391"},"modified":"2013-11-02T08:13:48","modified_gmt":"2013-11-02T16:13:48","slug":"make-a-new-string-on-the-fly","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=391","title":{"rendered":"Make a New String On-The-Fly"},"content":{"rendered":"<p>Never underestimate the power of the <em>printf()<\/em> function. It has amazing abilities to format output. And don&#8217;t forget about <em>printf()<\/em>&#8216;s lesser-known cousin, <em>sprintf()<\/em>. It can do amazing things with strings.<br \/>\n<!--more--><br \/>\nThe <em>sprintf()<\/em> function is nearly identical to <em>printf()<\/em>. It uses the same formatting power. The difference lies in where the string ends up: For <em>printf()<\/em>, the formatted string is sent to standard output. For <em>sprintf()<\/em>, however, the formatted string is saved to a character array (buffer) in memory. That&#8217;s powerful.<\/p>\n<p>The <em>printf()<\/em> function has this format:<\/p>\n<p><code>printf(\"<em>format<\/em>\"[,<em>args<\/em>...]);<\/code><\/p>\n<p><em>format<\/em> is the formatting string, or a string variable; <em>args<\/em> are the optional arguments, values or variables, one for each conversion character in the <em>format<\/em> string. You&#8217;re probably quite familiar with this format.<\/p>\n<p>The <em>sprintf()<\/em> function uses a similar format to <em>printf()<\/em>:<\/p>\n<p><code>sprintf(char *<em>buffer<\/em>,\"<em>format<\/em>\"[,<em>args<\/em>...]);<\/code><\/p>\n<p>The difference between <em>printf()<\/em> and <em>sprintf()<\/em>&#8216;s arguments is addition of a <em>char buffer<\/em>. That&#8217;s the address of the string created. So instead of being sent to standard output, the formatted string is saved to a location in memory.<\/p>\n<p>Here&#8217;s a quick example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char string[3];\r\n\r\n    sprintf(string,\"%d\",15*5);\r\n    printf(\"15 * 5 = \");\r\n    puts(string);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The buffer <em>string<\/em> at Line 5 has room for two characters, plus one character for the <code>\\0<\/code> (null) at the end of the string. <em>Remember that<\/em>!<\/p>\n<p>The <em>sprintf()<\/em> function at Line 7 calculates the result of 15 times 5, formats it as an integer by using <code>%d<\/code>, and saves the output in the <em>string<\/em> buffer. Lines 8 and 9 display the result.<\/p>\n<p>On your own, modify the code so that the entire string is formatted and saved to the <em>string<\/em> buffer and then output using a <em>puts()<\/em> function. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/11\/1116b.c\">Click here<\/a> to see my solution.<\/p>\n<p>The <em>sprintf()<\/em> function has lots of potential for building strings in memory. It can be used to convert numbers into strings, building strings with numbers, and even manipulate strings. It&#8217;s pretty powerful.<\/p>\n<p>For example, you could have use the <em>sprintf()<\/em> function as a solution for the <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=321\">October 2013 Exercise<\/a> on this blog.<\/p>\n<p>October&#8217;s puzzle was to insert the text <code>, nastiest<\/code> into the string <code>That's the biggest spider I've ever seen!<\/code> Here&#8217;s one way that task could be accomplished by using the <em>sprintf()<\/em> function:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char *text = \"That's the biggest spider I've ever seen!\";\r\n    char *insert = \", nastiest\";\r\n    char buffer[52];\r\n\r\n    sprintf(buffer,\"%.18s%s%s\",text,insert,text+18);\r\n    puts(buffer);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>That's the biggest, nastiest spider I've ever seen!<\/code><\/pre>\n<p>Yes, I cheated in the code by directly specifying the value 18 and the <code>buffer<\/code> size of 52. In a &#8220;real&#8221; program, you would want to calculate the values, but then the solution would be more along the lines of what&#8217;s presented for the October 13 exercise. My point here is to demonstrate the power of <em>sprintf()<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>sprintf()<\/em> function is a powerful tool you can use to build formatting strings while a program runs. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=391\">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-391","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\/391","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=391"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/391\/revisions\/398"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}