Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since your <code>return_val</code> is an <code>unsigned int</code>, you should probably be using <code>strtoul()</code> which has been standard since C89 and is therefore supported by MSVC (whereas <code>strtoll()</code> has only been standard since C99 and is not supported by MSVC).</p> <p>Your testing of the error conditions is not adequate. You need to set <code>errno</code> to zero before calling the conversion function; you also need to detect whether an error was reported, which is trickier than it seems.</p> <p>Section §7.20.1.4 'The strtol, strtoll, strtoul, and strtoull functions' of the C99 standard says:</p> <blockquote> <p><strong>Returns</strong></p> <p>The <code>strtol</code>, <code>strtoll</code>, <code>strtoul</code>, and <code>strtoull</code> functions return the converted value, if any. If no conversion could be performed, zero is returned. If the correct value is outside the range of representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is returned (according to the return type and sign of the value, if any), and the value of the macro ERANGE is stored in <code>errno</code>.</p> </blockquote> <p>You also have to read the look at the value stored in the <code>endptr</code> parameter to the conversion functions to tell that no conversion was performed (as opposed to a valid zero was converted).</p> <blockquote> <p>If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of <code>nptr</code> is stored in the object pointed to by <code>endptr</code>, provided that <code>endptr</code> is not a null pointer.</p> </blockquote> <p>So, you must write code more like this (omitting the test against EINVAL because the standard does not mention these functions setting <code>errno</code> to EINVAL):</p> <pre><code>unsigned int return_val=0; if (index + 1 &lt;= argc - 1) { char *end; unsigned long ul; errno = 0; ul = strtoul(argv[index+1], &amp;end, 10); if ((ul == 0 &amp;&amp; end == argv[index+1]) || (ul == ULONG_MAX &amp;&amp; errno == ERANGE) || (ul &gt; UINT_MAX)) { fprintf(stderr, "Could not parse argument %s for switch %s!\n", argv[index], argv[index+1]); return 0; } retval = (unsigned int)ul; } </code></pre> <p>Note that this is simpler than the test for a signed integer conversion which must take into account the negative <code>&lt;type&gt;_MIN</code> limit as well as the <code>&lt;type&gt;_MAX</code> limit.</p> <p>Also note that you really should record the result in an <code>unsigned long</code> and then check whether it fits within your designated range, which may be limited to UINT_MAX (which can be less than ULONG_MAX in a Unix-like 64-bit environment).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload