Ich versuche, die strtoul
Funktion zu verwenden, aber wie unten, damit er zurückkehrt, einen unerwarteten Wert (Zugabe von FFs am Anfang) gezeigt:strtoul geben unerwarteten Ausgang
#include <stdio.h>
#include <string.h>
#include <limits.h>
main() {
unsigned long temp ;
char *err;
temp = strtoul("3334444444",&err,10);
if (temp > UINT_MAX) {
printf("%lx %x %x\n",temp,3334444444,UINT_MAX);
}else
printf("%lx %x\n",temp,3334444444);
}
$./a.out
ffffffffc6bf959c c6bf959c ffffffff
Die obige Ausgabe entspricht dem if
Teil sein stimmt, obwohl ich erwarte, dass der else
Teil hier ausgeführt wird. Kann mir bitte jemand erklären, warum sich strtoul
so verhält? Warum gibt es ffffffffc6bf959c
statt nur c6bf959c
zurück? Wenn ich "333444444"
(d. H. Nur ein 4 weniger) anstelle von "3334444444"
in dem obigen Code verwende, dann erhalte ich die korrekte Ausgabe (d. H. 13dff55c 13dff55c
), die dem else
Teil entspricht.
Note : As pointed by melpomene in his reply below, stdlib.h header file should have been included and that will resolve the issue. Can anyone please let me know what is being done by the program by assuming the incorrect return type (int in this case) during compile time which can't be undone (or atleast it is not getting undone in this case) even after knowing the correct return type (unsigned long in this case) during link time ? In short, i want to know how c6bf959c is getting converted to ffffffffc6bf959c because of prototype not provided.
hast du versucht strtoull anstelle von strtoul? – bruceg
Ich bekomme Ihr erwartetes Verhalten mit diesem Code. Was ist deine Umgebung? –
'ULONG_MAX' hat dieselbe Größe wie' UINT_MAX' auf 32-Bit-Systemen. 'strtoull' wäre besser, wie Brueg sagte. – yellowantphil