
{"id":232,"date":"2018-09-20T15:35:15","date_gmt":"2018-09-20T15:35:15","guid":{"rendered":"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/?page_id=232"},"modified":"2018-09-20T15:38:55","modified_gmt":"2018-09-20T15:38:55","slug":"scanf-glossary-entry","status":"publish","type":"page","link":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/scanf-glossary-entry\/","title":{"rendered":"scanf (Glossary Entry)"},"content":{"rendered":"<p>One of the C standard library functions for reading <i>and<\/i> converting strings (sequences of ASCII characters) into other strings or numbers.\u00a0It is often used to read a keyboard, or data from a serial interface.\u00a0This is a complex function that can be difficult to master, made more difficult when input comes from a terminal (where different standards exist).<\/p>\n<blockquote><p>\u201cThe function scanf is the input analog of printf, providing many of the same conversion facilities in the opposite direction.<\/p>\n<p>int\u00a0scanf(char\u00a0*format,\u00a0&#8230;)<\/p>\n<p>scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments.\u201d<\/p>\n<p><i>Excerpt From: Brian W. Kernighan &amp; Dennis M. Ritchie. \u201cC Programming Language.\u201d\u00a0<\/i><\/p><\/blockquote>\n<p>The format parameter is a string containing formatting and conversion information, and &#8230; indicates a variable number of parameters (see below).<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>See \u201cTABLE 7-2. BASIC SCANF CONVERSIONS\u201d and \u201c7.3 Variable-length Argument Lists: in Brian W. Kernighan &amp; Dennis M. Ritchie. \u201cC Programming Language.\u201d<\/p>\n<p>The type char* is a pointer (address) to a list of characters in memory. This list (known as an array) must be terminated with the 8-bit value 0. In C, a short cut is to write the characters \u201cinside double quotes\u201d.<\/p>\n<p>Some examples are given below:<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">\/\/Read a string\r\nchar inputString[10]; \/\/Room for 9 characters + \\0\r\nint hits;\r\nint u;\r\n\r\nprintf(\"Type in an integer\\n\");\r\nscanf(\"%d\", &amp;u);\r\nprintf(\"You entered %d\\n\", u);\r\nprintf(\"One more is %d\\n\", u+1);<\/pre>\n<p>The above is one of the simplest examples. The input string (typically from a keyboard \/ serial interface, but not necessarily) must be an integer number written with ASCII characters (human readable). The format string %d looks for an string representation of an integer, converts it to an actual integer value and overwrites the variable u. Not that the address &amp;u is provided so that it can be modified in place.<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">printf(\"Enter the words: age=&lt;number&gt;, where &lt;number&gt; is an integer of your choice. No spaces!\\n\");\r\nhits = scanf(\"age=%d\", &amp;u);\r\nif (hits == 1) {\r\n   printf(\"Hello, I understand you are %d years old\\n\", u);\r\n} else {\r\n   printf(\"You did not do exactly what I asked\\n\");\r\n}<\/pre>\n<p>This example matches a specific pattern. The string \u201cage=\u201d followed immediately by an integer value. If this pattern is found, the scanf function returns the value 1.<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">printf(\"Type in a string wihtout spaces and press return\\n\");\r\nscanf(\"%9s\", inputString);\r\nprintf(\"You typed %s\\n\", inputString);\r\n\r\n\/\/Here are the ASCII characters\r\nfor (int n=0; n&lt;10; n++) {\r\n   printf(\"character %d = %d\\n\", n, inputString[n]);\r\n}<\/pre>\n<p>Here scanf is used to read a string of 9 characters length. Note that a C string always has a 0 on the end, so the destination must have 10 bytes or more space. Note that there is no &amp; in front of inputString. This is because inputString is an array, and giving its name returns the address of the first element.<\/p>\n<pre class=\"theme:xcode lang:c++ decode:true \">printf(\"Enter a name and an age, with a space between\\n\");\r\nscanf(\"%s %d\", inputString, &amp;u);\r\nprintf(\"Hello %s, I understand you are %d years old\\n\", inputString, u);\r\nHere scanf is using multiple format characters. The white-space is ignored.<\/pre>\n<p>See <b>Kernigan and Richie, Section 7.4 \u201cFormatted Input &#8211; Scanf\u201d <\/b>for more details.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the C standard library functions for reading and converting strings (sequences of ASCII characters) into other strings or numbers.\u00a0It is often used to read a keyboard, or data from a serial interface.\u00a0This is a complex function that can be difficult to master, made more difficult when input comes from a terminal (where different&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/scanf-glossary-entry\/\">Continue reading <span class=\"screen-reader-text\">scanf (Glossary Entry)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":153,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-232","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":1,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/232\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/232\/revisions\/233"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/153"}],"wp:attachment":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/media?parent=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}