<p>Using only ANSI C, what is the best way to, with fair certainty, determine if a C style string is either a integer or a real number (i.e float/double)?</p>

[{'answer_id': 78485, 'author': 'nutbar', 'author_id': 14425, 'author_profile': 'https://Stackoverflow.com/users/14425', 'pm_score': 2, 'selected': False, 'text': "<p>I suppose you could step through the string and check if there are any <code>.</code> characters in it.  That's just the first thing that popped into my head though, so I'm sure there are other (better) ways to be more certain.</p>\n"}, {'answer_id': 78487, 'author': 'itsmatt', 'author_id': 7862, 'author_profile': 'https://Stackoverflow.com/users/7862', 'pm_score': 2, 'selected': False, 'text': "<p>atoi and atof will convert or return a 0 if it can't.</p>\n"}, {'answer_id': 78540, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 1, 'selected': False, 'text': "<p>It really depends on why you are asking in the first place. </p>\n\n<p>If you just want to parse a number and don't know if it is a float or an integer, then just parse a float, it will correctly parse an integer as well.</p>\n\n<p>If you actually want to know the type, maybe for triage, then you should really consider testing the types in the order that you consider the most relevant. Like try to parse an integer and if you can't, then try to parse a float. (The other way around will just produce a little more floats...)</p>\n"}, {'answer_id': 78565, 'author': 'Patrick_O', 'author_id': 11084, 'author_profile': 'https://Stackoverflow.com/users/11084', 'pm_score': 6, 'selected': True, 'text': '<p>Don\'t use atoi and atof as these functions return 0 on failure. Last time I checked 0 is a valid integer and float, therefore no use for determining type.</p>\n\n<p>use the strto{l,ul,ull,ll,d} functions, as these set errno on failure, and also report where the converted data ended.</p>\n\n<p>strtoul: <a href="http://www.opengroup.org/onlinepubs/007908799/xsh/strtoul.html" rel="noreferrer">http://www.opengroup.org/onlinepubs/007908799/xsh/strtoul.html</a></p>\n\n<p>this example assumes that the string contains a single value to be converted.</p>\n\n<pre><code>#include &lt;errno.h&gt;\n\nchar* to_convert = "some string";\nchar* p = to_convert;\nerrno = 0;\nunsigned long val = strtoul(to_convert, &amp;p, 10);\nif (errno != 0)\n    // conversion failed (EINVAL, ERANGE)\nif (to_convert == p)\n    // conversion failed (no characters consumed)\nif (*p != 0)\n    // conversion failed (trailing data)\n</code></pre>\n\n<p>Thanks to Jonathan Leffler for pointing out that I forgot to set errno to 0 first.</p>\n'}, {'answer_id': 78569, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>atoi and atof will convert the number even if there are trailing non numerical characters.  However, if you use strtol and strtod it will not only skip leading white space and an optional sign, but leave you with a pointer to the first character not in the number.  Then you can check that the rest is whitespace.</p>\n'}, {'answer_id': 78580, 'author': 'Patrick', 'author_id': 429, 'author_profile': 'https://Stackoverflow.com/users/429', 'pm_score': 3, 'selected': False, 'text': '<p>Using <a href="http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html" rel="noreferrer">sscanf</a>, you can be certain if the string is a float or int or whatever without having to special case 0, as is the case with atoi and atof solution.</p>\n\n<p>Here\'s some example code:</p>\n\n<pre><code>int i;\nfloat f;\nif(sscanf(str, "%d", &amp;i) != 0) //It\'s an int.\n  ...\nif(sscanf(str "%f", &amp;f) != 0)  //It\'s a float.\n  ...\n</code></pre>\n'}, {'answer_id': 78627, 'author': 'Steve Jessop', 'author_id': 13005, 'author_profile': 'https://Stackoverflow.com/users/13005', 'pm_score': 2, 'selected': False, 'text': "<p>Use strtol/strtoll (not atoi) to check integers.\nUse strtof/strtod (not atof) to check doubles. </p>\n\n<p>atoi and atof convert the initial part of the string, but don't tell you whether or not they used all of the string. strtol/strtod tell you whether there was extra junk after the characters converted.</p>\n\n<p>So in both cases, remember to pass in a non-null TAIL parameter, and check that it points to the end of the string (that is, **TAIL == 0). Also check the return value for underflow and overflow (see the man pages or ANSI standard for details).</p>\n\n<p>Note also that strod/strtol skip initial whitespace, so if you want to treat strings with initial whitespace as ill-formatted, you also need to check the first character.</p>\n"}, {'answer_id': 80730, 'author': 'Jonathan Leffler', 'author_id': 15168, 'author_profile': 'https://Stackoverflow.com/users/15168', 'pm_score': 2, 'selected': False, 'text': "<p>I agree with Patrick_O that the strto{l,ul,ull,ll,d} functions are the best way to go.  There are a couple of points to watch though.</p>\n\n<ol>\n<li>Set errno to zero before calling the functions; no function does that for you.</li>\n<li>The Open Group page linked to (which I went to before noticing that Patrick had linked to it too) points out that errno may not be set.  It is set to ERANGE if the value is out of range; it <em>may</em> be set (but equally, <em>may</em> <em>not</em> be set) to EINVAL if the argument is invalid.</li>\n</ol>\n\n<p>Depending on the job at hand, I'll sometimes arrange to skip over trailing white space from the end of conversion pointer returned, and then complain (reject) if the last character is not the terminating null '\\0'.  Or I can be sloppy and let garbage appear at the end, or I can accept optional multipliers like 'K', 'M', 'G', 'T' for kilobytes, megabytes, gigabytes, terabytes, ... or any other requirement based on the context.</p>\n"}, {'answer_id': 21791935, 'author': 'Katie', 'author_id': 3312224, 'author_profile': 'https://Stackoverflow.com/users/3312224', 'pm_score': 0, 'selected': False, 'text': '<p>Well, if you don\'t feel like using a new function like strtoul, you could just add another strcmp statement to see if the string is 0.</p>\n\n<p>i.e.</p>\n\n<pre><code>if(atof(token) != NULL || strcmp(token, "0") == 0)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/78474', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9418/']