{"id":1010,"date":"2022-08-30T15:10:53","date_gmt":"2022-08-30T15:10:53","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/why-does-omitting-an-argument-to-printf-print-garbage-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:10:53","modified_gmt":"2022-08-30T15:10:53","slug":"why-does-omitting-an-argument-to-printf-print-garbage-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/why-does-omitting-an-argument-to-printf-print-garbage-collection-of-common-programming-errors\/","title":{"rendered":"Why does omitting an argument to printf print garbage?-Collection of common programming errors"},"content":{"rendered":"<p>I am using printf via assembly code. I note that in the following example if I ommit the expected argument, garbage is printed.<\/p>\n<pre><code>    .386\n    .model flat, c\n    .stack 100h\nprintf PROTO arg1:Ptr Byte, printlist:VARARG\n    .data\nmsg3fmt byte 0Ah,\"%s\",0Ah,\"test output\",0Ah,0\n    .code\nmain proc\n    INVOKE printf, ADDR msg3fmt\n    ret\nmain endp\n    end\n<\/code><\/pre>\n<p>My question is why? Is there a set memory address printf uses expecting to find an argument? Why is anything printed at all since no argument is passed?<\/p>\n<ol>\n<li>\n<p>The reason is that the format specifiers tell printf how many arguments it should have received. Printf gets its data from the stack; if you don&#8217;t provide any data for it then it will pull whatever happened to be on the stack and treat as an argument.<\/p>\n<\/li>\n<li>\n<p>The Standard says<\/p>\n<blockquote>\n<p>If the number of format specifiers in <code>printf()<\/code> is greater than the number of arguments the behavior is undefined.<\/p>\n<\/blockquote>\n<p>Undefined Behavior means <strong>anything<\/strong> can happen.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:13:10. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am using printf via assembly code. I note that in the following example if I ommit the expected argument, garbage is printed. .386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG .data msg3fmt byte 0Ah,&#8221;%s&#8221;,0Ah,&#8221;test output&#8221;,0Ah,0 .code main proc INVOKE printf, ADDR msg3fmt ret main endp end My question is why? Is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1010","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1010"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1010\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}