{"id":4335,"date":"2020-09-05T00:01:01","date_gmt":"2020-09-05T07:01:01","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4335"},"modified":"2020-09-12T08:17:27","modified_gmt":"2020-09-12T15:17:27","slug":"fully-abusing-the-scanf-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4335","title":{"rendered":"Fully Abusing the <em>scanf()<\/em> Function"},"content":{"rendered":"<p>It&#8217;s sad but understandable that most C programmers use the <em>scanf()<\/em> function in a limited capacity. True, the function is dorky, so I accept any aversion to its use. It can also be abused to pose a security risk. Still, for &#8220;research purposes,&#8221; it&#8217;s entertaining to see how much the function can be twisted and torqued.<br \/>\n<!--more--><br \/>\nThe key, introduced in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4313\">last week&#8217;s Lesson<\/a>, is to abuse the format string. Consider the following code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_09_05-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_09_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    char a,b,c;\r\n\r\n    printf(\"Type 'cat': \");\r\n    scanf(\"%c%c%c\",&amp;a,&amp;b,&amp;c);\r\n    printf(\"Got it: %c%c%c\\n\",a,b,c);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>scanf()<\/em> function at Line 8 thirsts for three single-character values, each one typed one after the other. This input isn&#8217;t a string, as its assigned to three <em>char<\/em> variables, <code>a<\/code>, <code>b<\/code>, and <code>c<\/code>. And, of course, you need not type <strong>cat<\/strong> for input, as the following sample run demonstrates:<\/p>\n<p><code>Type 'cat': dog<br \/>\nGot it: dog<\/code><\/p>\n<p>The next example is a wee bit more practical, though don&#8217;t get too excited:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_09_05-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_09_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    char name[16];\r\n    int score;\r\n\r\n    printf(\"Enter your name and score: \");\r\n    scanf(\"%s %d\",name,&amp;score);\r\n    printf(\"%s scored %d\\n\",name,score);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>scanf()<\/em> statement at Line 9 desires two separate input items, a string and an integer value (in decimal) separated by a space. These two input items must be typed properly, and you pray that string input doesn&#8217;t exceed 16 characters, or the code does all sorts of ugly things. Sample run:<\/p>\n<p><code>Enter your name and score: Danny 1000000<br \/>\nDanny scored 1000000<\/code><\/p>\n<p>True, it&#8217;s possible for <em>scanf()<\/em> to read more than one word in a string, providing you specify the spaces and other delimiting characters in the input format argument, as this code demonstrates:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_09_05-Lesson-c.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_09_05-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char first[16];\r\n    char last[16];\r\n\r\n    printf(\"Enter your name, first last: \");\r\n    scanf(\"%s %s\",first,last);\r\n    printf(\"Hello, %s %s\\n\",first,last);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 9, the <em>scanf()<\/em> function demands two words as input, separated by a space. Remember that for <em>scanf()<\/em>, string input is terminated by the first whitespace character encountered. By including the space character in the format string, you allow this specific whitespace character to flow in as input and not mess up everything. Here&#8217;s the obedient sample run:<\/p>\n<p><code>Enter your name, first last: Danny Gookin<br \/>\nHello, Danny Gookin<\/code><\/p>\n<p>I have one more <em>scanf()<\/em> trick to show, covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4341\">new week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While it can frustrate many programmers, you can have some fun with the <em>scanf()<\/em> function&#8217;s versatility. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4335\">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-4335","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\/4335","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=4335"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4335\/revisions"}],"predecessor-version":[{"id":4364,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4335\/revisions\/4364"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}