{"id":2086,"date":"2016-08-27T00:01:11","date_gmt":"2016-08-27T07:01:11","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2086"},"modified":"2016-08-20T09:31:34","modified_gmt":"2016-08-20T16:31:34","slug":"thats-not-the-input-i-asked-for","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2086","title":{"rendered":"That&#8217;s Not the Input I Asked For"},"content":{"rendered":"<p>I recently had a reader submit a question regarding input. He wanted to write code that would read a character and display that character&#8217;s ASCII code value. It sounds like a simple program, but you must understand variable types and the <em>scanf()<\/em> function&#8217;s peculiarities to properly code it.<br \/>\n<!--more--><br \/>\nHere is his code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int test;\r\n\r\n    scanf(\"%d\",&amp;test);\r\n    printf(\"%d\\n\",test);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>And here is sample output (keeping in mind that the code displays no prompts):<\/p>\n<pre><code>y\r\n1770414134<\/code><\/pre>\n<p>The value 1770414134 is what I see on my Mac. On the PC the value is 57, but 57 also shows up when I type <strong>z<\/strong> instead of <strong>y<\/strong>. I assume different computers would generate different results. Of course, I believe that the programmer was expecting to see the ASCII code value for the character; letter <code>'y'<\/code> is ASCII code 121 or <code>0x79<\/code>.<\/p>\n<p>The problem with the code is the <em>scanf()<\/em> function, which expects to read an integer, not a character:<\/p>\n<p><code>scanf(\"%d\",&test);<\/code><\/p>\n<p>Variable <code>test<\/code> is an <em>int<\/em> variable. The <code>%d<\/code> (integer) placeholder in the <em>scanf()<\/em> function is set properly. In fact, the code is correct. If you compile, run, and input the value <strong>121<\/strong>, you get:<\/p>\n<pre><code>121\r\n121<\/code><\/pre>\n<p>The value 121 is stored in variable <code>test<\/code>. It works!<\/p>\n<p>If you want to input a character, however, you must direct the <em>scanf()<\/em> function to look for a <strong>character<\/strong>. That means declaring <code>test<\/code> as a <em>char<\/em> variable and replacing the <code>%d<\/code> with a <code>%c<\/code>, the <em>char<\/em> placeholder. Here is the modified code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    char test;\r\n\r\n    scanf(\"%c\",&amp;test);\r\n    printf(\"%d\\n\",test);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>And when run:<\/p>\n<pre><code>y\r\n121<\/code><\/pre>\n<p>While his is a common mistake made by anyone learning C, the weird output piqued my curiosity regarding how the character <code>'y'<\/code> could be interpreted as value <code>1770414134<\/code>? What&#8217;s even more puzzling to me is that the value changed each time I ran the code; the output was never the same!<\/p>\n<p>The reason is simple: In the original code, the <em>scanf()<\/em> function is directed to read integer input. The character <code>'y'<\/code> isn&#8217;t a numeric value, to input isn&#8217;t read. Therefore, variable <code>test<\/code> is never initialized. The result you see is whatever garbage is in memory at variable <code>test<\/code>&#8216;s location.<\/p>\n<p>To prove that variable <code>test<\/code> is uninitialized, I modified the original code to initialize it:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int test = 0;\r\n\r\n    scanf(\"%d\",&amp;test);\r\n    printf(\"%d\\n\",test);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In Line 5, variable <code>test<\/code> is initialized to zero. If my theory is correct, the value of <code>test<\/code> won&#8217;t change when <code>'y'<\/code> is input.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>y\r\n0<\/code><\/pre>\n<p>I modified the code again, set <code>test<\/code> to 14, and the program&#8217;s output showed 14. That confirms that non-numeric input is ignored by <em>scanf()<\/em>.<\/p>\n<p>Bottom line: If you want to read a character, use a <em>char<\/em> variable and the <code>%c<\/code> placeholder in the <em>scanf()<\/em> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Experimentation can be fun, but sometimes leads to puzzling results. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2086\">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-2086","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\/2086","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=2086"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2086\/revisions"}],"predecessor-version":[{"id":2111,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2086\/revisions\/2111"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}