{"id":2351,"date":"2017-02-18T00:01:33","date_gmt":"2017-02-18T08:01:33","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2351"},"modified":"2017-02-25T08:19:55","modified_gmt":"2017-02-25T16:19:55","slug":"value-or-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2351","title":{"rendered":"Value or String?"},"content":{"rendered":"<p>My philosophy is to treat the input as a string until I can confirm that it&#8217;s not. The question then rises as to how strings can best translate into a value.<br \/>\n<!--more--><br \/>\nWhen specific input is required, you can use the <em>scanf()<\/em> function, which is really quite powerful &mdash; but it&#8217;s stubborn. The <em>scanf()<\/em> statement halts a program as it waits for a variable type that may never come.<\/p>\n<p>In my code, I just assume that all input is text. This assumption is safe because both values and text can be read as characters from the stream. I then use common library functions to translate values from text: <em>atoi()<\/em> for <em>int<\/em> values and <em>atof()<\/em> for <em>float<\/em> values. These functions do a great job translating text (ASCII) to values, interpreting weird input properly as either the value or as zero. They require the <code>stdlib.h<\/code> header file.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char *strings[4] = {\r\n        \"123\",\r\n        \"0.33\",\r\n        \"7lucky\",\r\n        \"string\"\r\n    };\r\n\r\n    puts(\"  String\\tAs int\\tAs float\");\r\n    for(int x=0;xlt;4;x++)\r\n    {\r\n        printf(\"%d: %-7s\\t%d\\t%f\\n\",\r\n                x+1,\r\n                strings[x],\r\n                atoi(strings[x]),\r\n                atof(strings[x]));\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Most of the ugliness in this code comes from the <em>printf()<\/em> statement starting at Line 16. The knots and bows in the formatting string help create a readable table of values. Here&#8217;s the program&#8217;s output:<\/p>\n<pre><code>  String\tAs int\tAs float\r\n1: 123    \t123\t123.000000\r\n2: 0.33   \t0\t0.330000\r\n3: 7lucky \t7\t7.000000\r\n4: string \t0\t0.000000<\/code><\/pre>\n<p>If your code had to read the four sample strings as input, the translation done by <em>atoi()<\/em> and <em>atof()<\/em> wouldn&#8217;t clog the pipes. But if you want to know whether a string is specifically an <em>int<\/em> or <em>float<\/em>, more processing is necessary.<\/p>\n<p>As an example, consider a CSV file where you don&#8217;t know the data fields. Now that&#8217;s a bad situation anyway, but just pretend: Your code reads in a string and then must determine the best format: <em>char<\/em> array, <em>int<\/em>, or <em>float<\/em>. Yes, it&#8217;s a guess, but you can make a <em>good<\/em> guess.<\/p>\n<p>I can think of several approaches to solve this puzzle.<\/p>\n<p>In one method, you can use <em>atoi()<\/em> or <em>atof()<\/em> and then compare the value with the original string. The <em>sprintf()<\/em> function can be used to translate the value back into a string, then run <em>strcmp()<\/em> on both to confirm the results.<\/p>\n<p>Another way would be to examine the string character-by-character. If the string is all numbers, it&#8217;s probably an <em>int<\/em>. If the string is all numbers but also has a decimal point, it&#8217;s probably a <em>float<\/em>. Otherwise, the string is a string. Then again, the string <code>7lucky<\/code> in the example did translate into the values 7 and 7.0, but that might not be what you consider a valid value.<\/p>\n<p>In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2379\">next week&#8217;s Lesson<\/a>, I&#8217;ll present code that demonstrates both of these solutions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You read data as text, but does it translate into a value or into a string? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2351\">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-2351","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\/2351","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=2351"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2351\/revisions"}],"predecessor-version":[{"id":2394,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2351\/revisions\/2394"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}