Das folgende C++ Programm ruft strtoul
von negativen 1. Da keine negativen Zahlen in einem unsigned Typ darstellbar sind, hatte ich erwartet, dass dies fehlschlägt und 0 zurückgibt (If no valid conversion could be performed, a zero value is returned.), sondern eine große positive Zahl wird zurückgegeben (If the value read is out of the range of representable values by an unsigned long int, the function returns ULONG_MAX
(defined in <climits>
), and errno
is set to ERANGE
.).C/C++ strtoul negative Zahl
#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;}
}
Warum schlägt strtoul nicht fehl und gibt 0 für negative Zahlen zurück?
Was ist Ihr Betriebssystem? Compiler? C/C++ Laufzeit? –
@AndrewHenle. Irrelavant in diesem Fall. Die Funktion demonstriert korrektes Verhalten. –
@Galik. Nicht wahr. Es gibt ** nur ** vorzeichenlose Zahlen zurück. –