{"id":4313,"date":"2020-08-29T00:01:01","date_gmt":"2020-08-29T07:01:01","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4313"},"modified":"2020-09-05T08:47:07","modified_gmt":"2020-09-05T15:47:07","slug":"unraveling-the-scanf-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4313","title":{"rendered":"Unraveling the <em>scanf()<\/em> Function"},"content":{"rendered":"<p>One of the things I dread in my books, is foisting the <em>scanf()<\/em> function upon a beginner. I have no way around this frustration.<br \/>\n<!--more--><br \/>\nThe <em>scanf()<\/em> function is useful in that it cleanly gathers input, converting it into a value. It&#8217;s horrid for reading strings, but the function is vital for basic input and getting a beginner started. My issue is that the format is straight from the fiery pits of Hell, darkly illustrated in Figure 1.<\/p>\n<div id=\"attachment_4322\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4322\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0829-figure1.png\" alt=\"The scanf() function&#039;s arguments\" width=\"550\" height=\"188\" class=\"size-full wp-image-4322\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0829-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0829-figure1-300x103.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/08\/0829-figure1-500x171.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-4322\" class=\"wp-caption-text\">Figure 1. How the <em>scanf()<\/em> function&#8217;s arguments are put together.<\/p><\/div>\n<p>Here you go, eager beginner! Good luck! But let me explain what&#8217;s going on.<\/p>\n<p>I believe beginners have a good handle on the function thing: They recognize that <em>scanf()<\/em> is a function. They can even stomach the two arguments, separated by a comma. But these are the most wily arguments possible: <code>\"%d\"<\/code> and <code>&a<\/code>.<\/p>\n<p>The first mystery item is a format string. It uses cryptic placeholders to represent the type of data requested.<\/p>\n<p>The second mystery item is the address of a variable, a concept relying upon a knowledge of pointers. No need to mention that pointers are the most terrifying aspect of C or any programming language, a topic postponed well into adulthood and completely out of place at the start of any training material. Yet here it is.<\/p>\n<p>Like most cryptic things, <em>scanf()<\/em> is powerful. Its flexibility allows you to comb standard input, setting whatever text flows in to a given variable. This variable must be passed as a pointer argument; it would subtract from <em>scanf()<\/em>&#8216;s usefulness to otherwise assign it a specific data type, say to instead return the input value. Therefore the function must be cryptic to a degree because the data type placeholder must match the variable&#8217;s data type, both of which must be flexible.<\/p>\n<p>Beginners can tolerate <em>scanf()<\/em> as they learn, but I desire to dive deeper. Consider the format string. It&#8217;s pretty much the same as the <em>printf()<\/em> format string, so must the input match exactly?<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_08_29-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_08_29_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 a;\r\n\r\n    printf(\"How old are you? \");\r\n    scanf(\"I am %d years old\",&amp;a);\r\n    printf(\"I'm %d, too!\\n\",a);\r\n    \r\n    return(0);\r\n}<\/pre>\n<p>The <em>scanf()<\/em> format string at Line 8 contains some text in addition to the placeholder. This specific format restricts input only to text that matches.<\/p>\n<p>For my first sample run, I just typed in a value:<\/p>\n<p><code>How old are you? 29<br \/>\nI'm 266395685, too!<\/code><\/p>\n<p>Oops. The format string demands more text input. Here&#8217;s my second sample run:<\/p>\n<p><code>How old are you? I am 29 years old<br \/>\nI'm 29, too!<\/code><\/p>\n<p>In this run, I typed the full text, <code>I am 29 years old<\/code>. This is the input <em>scanf()<\/em> is looking for. It translates the only value, but the rest of the text input is necessary for proper interpretation.<\/p>\n<p>Next, I tried to mess with the function by typing only the first part of the format string:<\/p>\n<p><code>How old are you? I am 29<\/p>\n<p>years old<br \/>\nI'm 29, too!<\/code><\/p>\n<p>After typing <code>I am 29<\/code> I pressed Enter. Nothing. Enter again. Nothing. Only when I typed the rest of the format string did <em>scanf()<\/em> finally process the input.<\/p>\n<p>This formatting string quirk isn&#8217;t the only oddity with <em>scanf()<\/em>. Another way to abuse this function is covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4335\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most despised functions in C, <em>scanf()<\/em> is both useful and mysterious. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4313\">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-4313","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\/4313","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=4313"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4313\/revisions"}],"predecessor-version":[{"id":4355,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4313\/revisions\/4355"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}