I met some unexcepted result of strtol
in c
Here is the sample program.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("%x\n", strtol("0xfffff70A", NULL, 0));
return 0;
}
and the output of this simple program is
0x7fffffff
rather than 0xfffff70A
.
And if I use strtoul
, the result is exactly 0xfffff70a
.
I am using a 32-bit machine, and I wonder what happens.
PS. I am using gcc 4.7.2
errno
to0
before callingstrtol
orstrtoul
and check it afterwards. Do not use the value iferrno
has a different value. – Maryrosemarys