{"id":5213,"date":"2022-02-26T00:01:57","date_gmt":"2022-02-26T08:01:57","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5213"},"modified":"2022-02-19T10:53:50","modified_gmt":"2022-02-19T18:53:50","slug":"how-many-characters-output","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5213","title":{"rendered":"How Many Characters Output?"},"content":{"rendered":"<p>I don&#8217;t think I&#8217;ve ever used the return value from a <em>printf()<\/em> statement. Yes, the function has an <em>int<\/em> return value, which is the number of characters output. This value can be difficult to guess.<br \/>\n<!--more--><br \/>\nHere&#8217;s your sample code, which I use throughout this Lesson:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_02_26-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_02_26-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int r;\r\n\r\n    r = printf(\"Goodbye, cruel world!\\n\");\r\n\r\n    printf(\"%d characters output\\n\",r);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program outputs a string, then the number of characters output:<\/p>\n<p><code>Goodbye, cruel world!<br \/>\n22 characters output<\/code><\/p>\n<p>The string <code>\"Goodbye, cruel world!\\n\"<\/code> contains 23 characters when you count the <code>\\n<\/code> escape sequence as two characters. But the compiler sees it as a single character (the newline), which is what&#8217;s output. The <em>printf()<\/em> function accurately returns the number of characters output.<\/p>\n<p>Now change Line 7 in the code to read:<\/p>\n<p><code>r = printf(\"%d * %d = %d\\n\",400,500,400*500);<\/code><\/p>\n<p>The format string contains 14 characters, of which 13 are single characters (remember that <code>\\n<\/code> is one character). The output, however, is longer:<\/p>\n<p><code>400 * 500 = 200000<br \/>\n19 characters output<\/code><\/p>\n<p>A diversion C programming nerds like to play on each other is to guess the <em>printf()<\/em> statement&#8217;s return value. The format string can get quite complex, which thrills the nerds to no end:<\/p>\n<p><code>r = printf(\"%*s\\n\",34,\"Wow!\");<\/code><\/p>\n<p>The above <em>printf()<\/em> statement uses the <code>*<\/code> <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2972\">variable width item<\/a> in the <code>%s<\/code> placeholder. The output is based on the second argument 34. So the string <code>\"Wow!\"<\/code> is output within a chunk of spaces 34 characters wide, right-justified. Then comes the newline.<\/p>\n<p>Can you guess the value of variable <code>r<\/code> after the above statement executes?<\/p>\n<p><em>[tick-tock&#8230;]<\/em><\/p>\n<p>My first guess &mdash; impulsive and irrational &mdash; was 38. Wrong. I figured 34 spaces plus <code>\"Wow!\"<\/code> and I missed the newline. But this incorrect and hasty assumption forgot how the <code>*<\/code> wildcard works. Regardless, run the code to view the correct answer:<\/p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Wow!<br \/>\n35 characters output<\/code><\/p>\n<p>If the <code>*<\/code> width is 34, the string output is always 34 characters long, plus one for the newline. The return value confirms the number of characters output.<\/p>\n<p>As I wrote earlier, I don&#8217;t recall ever using the <em>printf()<\/em> return value in my code. I&#8217;ve never had a practical reason for its application. After all, it seems a bit tardy: If you&#8217;re concerned about outputting characters of a given width, the value returned from <em>printf()<\/em> is available only after the text is output.<\/p>\n<p>The <em>man<\/em> page explains that most <em>printf()<\/em> family functions return the number of characters output, minus the trailing null character, <code>\\0<\/code>. The <em>snprintf()<\/em> and <em>vsnprintf()<\/em> functions are different: They return the number of characters that would be output if the output size weren&#8217;t restricted.<\/p>\n<p>While its return value offers the exact number of characters output, another feature of the <em>printf()<\/em> function allows you to monitor the number of characters output at a given position within the format string. This oddball is the <code>%n<\/code> conversion character &mdash; the use of which is fraught with danger and intrigue. I write more about this placeholder in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Though it&#8217;s seldom used, the <em>printf()<\/em> function has a return value. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5213\">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-5213","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\/5213","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=5213"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5213\/revisions"}],"predecessor-version":[{"id":5225,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5213\/revisions\/5225"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}