Discussion:
strtoll question
Emmanuel Dreyfus
2014-09-15 04:35:58 UTC
Permalink
Hello

Consider the program below and its output. Am I using strtoll() wrongly,
or is this a bug? (this is netbsd-7 branch)

$ cat /tmp/x.c
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>

int
main(void)
{
const char *ptr = "999999999999999";
char *endp;
long long int res;

errno = 0;
res = strtoll(ptr, &endp, 0);
printf("errno = %d\n", errno);
printf("*endp = 0x%02x\n", *endp);
printf("str %s\n", ptr);
printf("num %lld\n", res);

return 0;
}

$ cc -o /tmp/x /tmp/x.c
$ /tmp/x
errno = 0
*endp = 0x00
str 999999999999999
num -1530494977
--
Emmanuel Dreyfus
http://hcpnet.free.fr/pubz
***@netbsd.org
Matt Thomas
2014-09-15 05:03:34 UTC
Permalink
Post by Emmanuel Dreyfus
Hello
Consider the program below and its output. Am I using strtoll() wrongly,
or is this a bug? (this is netbsd-7 branch)
$ cat /tmp/x.c
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
int
main(void)
{
const char *ptr = "999999999999999";
char *endp;
long long int res;
errno = 0;
res = strtoll(ptr, &endp, 0);
printf("errno = %d\n", errno);
printf("*endp = 0x%02x\n", *endp);
printf("str %s\n", ptr);
printf("num %lld\n", res);
return 0;
}
$ cc -o /tmp/x /tmp/x.c
$ /tmp/x
errno = 0
*endp = 0x00
str 999999999999999
num -1530494977
No. You have no prototype for strtoll in scope.

(gdb) print (int) 999999999999999
$1 = -1530494977

You are missing #include <stdlib.h>

-Wall is your friend.

a.c:14:8: warning: implicit declaration of function 'strtoll' [-Wimplicit-function-declaration]
res = strtoll(ptr, &endp, 0);
Emmanuel Dreyfus
2014-09-15 07:09:50 UTC
Permalink
Post by Matt Thomas
You are missing #include <stdlib.h>
Thanks!
--
Emmanuel Dreyfus
***@netbsd.org
Loading...