{"id":646,"date":"2014-04-19T00:01:06","date_gmt":"2014-04-19T07:01:06","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=646"},"modified":"2014-11-22T10:10:32","modified_gmt":"2014-11-22T18:10:32","slug":"scanf-the-bastard","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=646","title":{"rendered":"<em>scanf()<\/em> the Bastard"},"content":{"rendered":"<p>When you first learn the C language, and you&#8217;re practicing basic input\/output, you become familiar with the two I\/O workhorses of the C library: <em>printf()<\/em> and <em>scanf()<\/em>. Of the two, <em>printf()<\/em> is my favorite. The <em>scanf()<\/em> function is highly useful, and it&#8217;s a great learning tool, but it&#8217;s not the best function for reading in a string of text.<br \/>\n<!--more--><br \/>\nThe <em>scanf()<\/em> function scans <em>formatted<\/em> input. It uses the same placeholders, or conversion characters, as the <em>printf()<\/em> function. So when your code desires an <em>unsigned int<\/em> value, you use the <code>%u<\/code> and <em>scanf()<\/em> grabs only that value. In fact, it diligently rejects any non <em>unsigned int<\/em> value. (That&#8217;s a feature that can throw off some programmers.)<\/p>\n<p>When used with the <code>%s<\/code> placeholder, <em>scanf()<\/em> reads in strings &#8212; but only up until the first white space character. That limits <em>scanf()<\/em>&#8216;s definition of a string, which is frustrating.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char name[64];\r\n    char color[24];\r\n\r\n    printf(\"Your first and last name: \");\r\n    scanf(\"%s\",name);\r\n    printf(\"Your favorite color: \");\r\n    scanf(\"%s\",color);\r\n\r\n    printf(\"%s's favorite color is %s.\\n\",\r\n        name,\r\n        color);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>If you type in and run the above code, you may experience the following indigestion while running the program:<\/p>\n<p><code>Your first and last name: <span style=\"color:red\">Dan Gookin<\/span><br \/>\nYour favorite color: Dan's favorite color is Gookin.<\/code><\/p>\n<p>Because the text <code>Dan Gookin<\/code> contains a space, the <em>scanf()<\/em> function stops reading after the first word, <code>Dan<\/code>. The rest of the text remains in the input stream, where it&#8217;s read by the second <em>scanf()<\/em> function and assigned to the <code>color<\/code> variable. The output is predicatble &#8212; but only if you&#8217;re familiar with the nature of the <em>scanf()<\/em> function.<\/p>\n<p>Can you work around this limitation?<\/p>\n<p>No!<\/p>\n<p>Don&#8217;t even try to fake out <em>scanf()<\/em> and attempt to read in more text or avoid the issue. You can&#8217;t modify input nor can you fool the function in other ways.<\/p>\n<p>Now I&#8217;ve read that some compliers may allow the <code>*<\/code> wildcard to be inserted into the <code>%s<\/code> placeholder. That trick doesn&#8217;t work on any of my machines, therefore I wouldn&#8217;t rely upon it for squat.<\/p>\n<p>If you really need to read in a full string of text, then use a proper function. For example, the <em>fgets()<\/em> function can read text from the input stream. You can read about this function in my books, as well as my <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=24\">June 1, 2013 Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>scanf()<\/em> function is a basic input function, but it&#8217;s not the best for grabbing strings of text. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=646\">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-646","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\/646","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=646"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/646\/revisions"}],"predecessor-version":[{"id":1121,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/646\/revisions\/1121"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}