以下C++程序調用了負1的strtoul
。由於在任何無符號類型中都沒有負數可表示,所以我期望這會失敗並返回0(If no valid conversion could be performed, a zero value is returned.),而是一個較大的正數返回(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
#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;}
}
爲什麼strtoul不會失敗並返回0爲負數?
什麼是你的操作系統明顯不同?編譯器? C/C++運行時? –
@AndrewHenle。在這種情況下不相關。該功能表明正確的行爲。 –
@Galik。不對。它只返回**無符號數字。 –