{"id":5223,"date":"2022-03-05T00:01:46","date_gmt":"2022-03-05T08:01:46","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5223"},"modified":"2022-02-26T09:37:54","modified_gmt":"2022-02-26T17:37:54","slug":"how-many-characters-output-so-far","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5223","title":{"rendered":"How Many Characters Output &#8211; So Far?"},"content":{"rendered":"<p>I doubt any C programmer has memorized all of the <em>printf()<\/em> function&#8217;s placeholders, options, and settings. It&#8217;s a character salad of interesting and weird things. One of the strangest has to be the <code>%n<\/code> placeholder.<br \/>\n<!--more--><br \/>\nFor a placeholder review, refer to my series of Lessons on Conversion Character Mania, which <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=288\">starts here<\/a>.<\/p>\n<p>From <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5213\">last week&#8217;s Lesson<\/a>, the <em>printf()<\/em> function returns the number of characters output. The <code>%n<\/code> placeholder, used within the format string, returns the number of characters output so far.<\/p>\n<p>Suppose that the format string is <code>\"123%n456\"<\/code>. If so, the <code>%n<\/code> placeholder represents the value three, the number of characters output before its position in the string. The value reflects output, so another placeholder appearing before <code>%n<\/code> affects the value returned.<\/p>\n<p>To make the <code>%n<\/code> placeholder work, its corresponding argument must be the address of an <em>int<\/em> value. The following code demonstrates:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_03_05-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2022_03_05-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int n;\r\n\r\n    printf(\"Oh no!%n Here comes Godzilla!\\n\",&amp;n);\r\n    printf(\"%d\\n\",n);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Integer variable <code>n<\/code> is declared at Line 5. Its address is referenced in the <em>printf()<\/em> statement at Line 7. The value stored is the number of characters output before the <code>%n<\/code> placeholder appears (most likely six).<\/p>\n<p>At Line 8, the value of variable <code>n<\/code> is output.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Oh no! Here comes Godzilla!<br \/>\n6<\/code><\/p>\n<p>Yup. It&#8217;s six.<\/p>\n<p>The disappointing news is that you cannot access this value until after the <em>printf()<\/em> statement returns. So the following code doesn&#8217;t do what you think it does:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_03_05-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2022_03_05-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int n;\r\n\r\n    printf(\"The total is %d!%n\\n%d characters output above\\n\",\r\n            500*80,\r\n            &amp;n,\r\n            n\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>%n<\/code> placeholder appears after the exclamation point in the <em>printf()<\/em> format string at Line 7. Its argument appears at Line 9, but the value is used at Line 10. This value is unset, as the output proves:<\/p>\n<p><code>The total is 40000!<br \/>\n24342624 characters output above<\/code><\/p>\n<p>The number 24342624 represents whatever garbage is in memory before variable <code>n<\/code> is initialized. That&#8217;s because the value is initialized after the function returns.<\/p>\n<p>Add the following statement at Line 12:<\/p>\n<p><code>printf(\"n==%d\\n\",n);<\/code><\/p>\n<p>Save, build, and run to see this output:<\/p>\n<p><code>The total is 40000!<br \/>\n24342624 characters output above<br \/>\nn==19<\/code><\/p>\n<p>Seeing how the <code>%n<\/code> placeholder&#8217;s value isn&#8217;t available until after the function executes makes this placeholder seem less-than-useful to me. While you could use this value and the <em>printf()<\/em> function&#8217;s return value to do some character output math, the <code>%n<\/code> placeholder does have a warning attached to it.<\/p>\n<p>It&#8217;s possible that the <code>%n<\/code> placeholder can be used to nefariously write data to a sensitive memory location. The method is complex, but the format string can be manipulated in a way to access memory in a malicious manner. Even for secure functions such as s<em>nprintf()<\/em>, avoid using a string variable to set the format string when the <code>%n<\/code> placeholder is used.<\/p>\n<p>I believe the warning about the <code>%n<\/code> placeholder outweighs any useful value it may have, though I remain curious which type of code would require this placeholder in the first place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The obscure and potentially dangerous <code>%n<\/code> placeholder deserves an explanation. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5223\">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-5223","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\/5223","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=5223"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5223\/revisions"}],"predecessor-version":[{"id":5240,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5223\/revisions\/5240"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}