Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Wolf <eric@boese-wolf.eu> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: errno |
| Date | 2014-04-26 23:11 +0200 |
| Message-ID | <bs2lo7FsnqU1@mid.individual.net> (permalink) |
| References | <ljh687$b06$1@dont-email.me> |
First:
The line
> printf("%d\n", a);
seems to be wrong. As a is long int, it should read
printf("%ld\n", a);
^ mind the 'l'
Second:
As I understand 'man strtol' on my system, strtol sets errno only in
case of underflow or overflow, in which case it returns LONG_MIN or
respectively LONG_MAX.
So checking for -1 doesn't detect an error situation. You should
check for LONG_MAX and errno != 0 and LONG_MIN and errno != 0, or
simply errno != 0.
Posix manpage 'man 3posix strtol' on my system, says also a return value
of 0 could indicate an error if errno != 0 additionally.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main(int argc, char *argv[])
{
long int a;
errno = 0;
a = strtol(argv[1], NULL, 16);
if (errno != 0)
printf("%s\n", strerror(errno));
printf("%ld\n", a);
return 0;
}
Should print
Numerical result out of range
9223372036854775807
for in input of
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
Eric
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
errno "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-26 16:51 -0400
Re: errno Eric Wolf <eric@boese-wolf.eu> - 2014-04-26 23:11 +0200
Re: errno "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-26 17:17 -0400
Re: errno Ike Naar <ike@iceland.freeshell.org> - 2014-04-26 22:35 +0000
Re: errno "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-26 19:35 -0400
Re: errno Richard <rgrdev_@gmail.com> - 2014-04-26 23:56 +0200
Re: errno Barry Schwarz <schwarzb@dqel.com> - 2014-04-26 17:15 -0700
Re: errno "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-26 20:23 -0400
Re: errno "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-26 20:27 -0400
Re: errno "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-26 20:42 -0400
csiph-web