The following C++ program calls strtoul of negative 1. Since no negative numbers are representable in any unsigned type, I had expected this to fail and retu 0 (If no valid conversion could be performed, a zero value is retued.), but instead a large positive number is retued (If the value read is out of the range of representable values by an unsigned long int, the function retus ULONG_MAX (defined in ), and ero is set to ERANGE.).
#include <cstdlib>
#include <iostream>
int main () {
{char s[] = "-1";
for (int b=0; b<17; ++b)
std::cout << "strtoul (unsigned) of " << s
<< " with base arg " << b
<< ": " << strtoul(s,0,b) << std::endl;}
}
Why does strtoul not fail and retu 0 for negative numbers?
