{"id":1120,"date":"2014-12-20T00:01:36","date_gmt":"2014-12-20T08:01:36","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1120"},"modified":"2015-01-03T07:37:51","modified_gmt":"2015-01-03T15:37:51","slug":"c-language-string-reading-function-overview","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1120","title":{"rendered":"C Language String Reading Function Overview"},"content":{"rendered":"<p>When the C Lords banished the <em>gets()<\/em> function into obscurity, cohorts of confused coders descended upon the <em>fgets()<\/em> function as a viable alternative. Alas, <em>fgets()<\/em> isn&#8217;t without its faults.<br \/>\n<!--more--><br \/>\nWhile <em>fgets()<\/em> is basically the <em>gets()<\/em> function with an input cap, it has one major difference: The <em>fgets()<\/em> function stores the Enter key character (newline or <code>\\n<\/code>) at the end of the string. That makes it different from the <em>gets()<\/em> function, but it also requires overhead when you want to to deal with that extra character.<\/p>\n<p>I suggest a fix to the <em>fgets()<\/em> function in this <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=24\">Lesson<\/a> from June, 2013.<\/p>\n<p>The other string input function despondent programmers turned to was <em>scanf()<\/em>, which isn&#8217;t really a string input function at all.<\/p>\n<p>While <em>scanf()<\/em> is great for reading in discrete values, such as integers and floats, and it&#8217;s handy when you learn to program, the function doesn&#8217;t interface well with humans. It&#8217;s much better to receive human input in the form of a string, then deal with that input inside the code. (I&#8217;ll provide a specific rant about how dealing with human input in a future Lesson.)<\/p>\n<p>The key thing to remember about <em>scanf()<\/em> and strings is that it stops reading characters at the first sign of whitespace.<\/p>\n<p>Whitespace characters include the space, tab, and Enter.<\/p>\n<p>That limitation means that <em>scanf()<\/em> can read words just fine, but not strings. Not unless you go all Roman and write everything without any spaces.<\/p>\n<blockquote><p>GALLIAESTOMNISDIVISAINPARTESTRES<\/p><\/blockquote>\n<p>I&#8217;m sure Caesar wouldn&#8217;t notice anything wrong with <em>scanf()<\/em> and strings.<\/p>\n<p>Anyway, I wax poetic on <em>scanf()<\/em> in this <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=646\">Lesson<\/a> from April 2014.<\/p>\n<p>You can also craft your own input function, which I strongly recommend. I&#8217;ll demonstrate such a function in the <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1123\">next Lesson<\/a>.<\/p>\n<p>Also, the C library sports a solid input function <em>getline()<\/em>, which I&#8217;ll cover in a <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1112\">future Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When the C Lords banished the gets() function into obscurity, cohorts of confused coders descended upon the fgets() function as a viable alternative. Alas, fgets() isn&#8217;t without its faults.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1120","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\/1120","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=1120"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1120\/revisions"}],"predecessor-version":[{"id":1162,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1120\/revisions\/1162"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}