According to the documentation for strtoul
, regarding its return value...
This function returns the converted integral number as a long int value. If no valid conversion could be performed, a zero value is returned.
What if I'm parsing a user-supplied string of "0" where, for my application, "0" may be a valid entry? In that case it seems that I have no way to determine from using strtoul
if a valid conversion was performed. Is there another way to handle this?
Any value returned from
strtoul()
may be from an expected string input or from other not so expected strings. Further tests are useful.The following strings all return 0 from
strtoul()
"0"
,"-0"
,"+0"
""
,"abc"
" 0"
"0xyz"
,"0 "
,"0.0"
strtoul()
has the various detection modes.What is not yet detected.
Negative input.
strtoul()
effectively wraps around such thatstrtoul("-1", 0, 10) == ULONG_MAX)
. This issue is often missed in cursory documentation review.Leading white space allowed. This may or may not be desired.
To also detect negative values: