Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #43611 > unrolled thread
| Started by | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| First post | 2014-04-26 16:51 -0400 |
| Last post | 2014-04-26 20:42 -0400 |
| Articles | 10 — 5 participants |
Back to article view | Back to comp.lang.c
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
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2014-04-26 16:51 -0400 |
| Subject | errno |
| Message-ID | <ljh687$b06$1@dont-email.me> |
My little utility doesn't print an errno value like it should. I have
tried to fix it but have failed. I've never really used errno.h but always
perror() so what am I doing wrong here.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main(int argc, char *argv[])
{
long int a;
errno = 0;
if ((a = strtol(argv[1], NULL, 16)) == -1)
printf("%s\n", strerror(errno));
printf("%d\n", a);
return 0;
}
[toc] | [next] | [standalone]
| From | Eric Wolf <eric@boese-wolf.eu> |
|---|---|
| Date | 2014-04-26 23:11 +0200 |
| Message-ID | <bs2lo7FsnqU1@mid.individual.net> |
| In reply to | #43611 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2014-04-26 17:17 -0400 |
| Message-ID | <ljh7pp$ksq$1@dont-email.me> |
| In reply to | #43612 |
"Eric Wolf" <eric@boese-wolf.eu> wrote in message
news:bs2lo7FsnqU1@mid.individual.net...
> 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'
Never really used long int either much.
Usually go to atoi().
> 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
Thanks much! I typed
/h2d fffffffffffffff
And all that was printed was -1. I'm used to thinking that's an error but
there is those few weird functions...
Bill
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Date | 2014-04-26 22:35 +0000 |
| Message-ID | <slrn3vfsllod5p.p4i.ike@iceland.freeshell.org> |
| In reply to | #43614 |
On 2014-04-26, Bill Cunningham <nospam@nspam.invalid> wrote:
>> #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;
>> }
> Thanks much! I typed
>
> /h2d fffffffffffffff
> And all that was printed was -1. I'm used to thinking that's an error but
> there is those few weird functions...
That is why you should have used "%ld" instead of "%d" to
print the value of a. Had you used "%ld", the output would
not have been -1 but the equivalent of LONG_MAX.
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2014-04-26 19:35 -0400 |
| Message-ID | <ljhfr9$6rj$1@dont-email.me> |
| In reply to | #43618 |
"Ike Naar" <ike@iceland.freeshell.org> wrote in message
news:slrn3vfsllod5p.p4i.ike@iceland.freeshell.org...
> That is why you should have used "%ld" instead of "%d" to
> print the value of a. Had you used "%ld", the output would
> not have been -1 but the equivalent of LONG_MAX.
Got it now. All I have really used was atoi and it doesn't return error.
I might just go to errno.h instead of simple perror().
Bill
[toc] | [prev] | [next] | [standalone]
| From | Richard <rgrdev_@gmail.com> |
|---|---|
| Date | 2014-04-26 23:56 +0200 |
| Message-ID | <87ppk3n4b1.fsf@gmail.com> |
| In reply to | #43611 |
"Bill Cunningham" <nospam@nspam.invalid> writes:
> My little utility doesn't print an errno value like it should. I have
> tried to fix it but have failed. I've never really used errno.h but always
> perror() so what am I doing wrong here.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <errno.h>
> #include <string.h>
>
> int main(int argc, char *argv[])
> {
> long int a;
> errno = 0;
> if ((a = strtol(argv[1], NULL, 16)) == -1)
> printf("%s\n", strerror(errno));
> printf("%d\n", a);
> return 0;
> }
>
0/10
Up yer game Bill. Even the regs who are chomping at the bit to show off
aren't biting anymore.
--
"Avoid hyperbole at all costs, its the most destructive argument on
the planet" - Mark McIntyre in comp.lang.c
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2014-04-26 17:15 -0700 |
| Message-ID | <dsgol9h34hqo6q4jk00fl6boj6mneb6n4v@4ax.com> |
| In reply to | #43611 |
On Sat, 26 Apr 2014 16:51:17 -0400, "Bill Cunningham"
<nospam@nspam.invalid> wrote:
> My little utility doesn't print an errno value like it should. I have
Nowhere in your code do you attempt to print the value of errno.
>tried to fix it but have failed. I've never really used errno.h but always
>perror() so what am I doing wrong here.
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <errno.h>
>#include <string.h>
>
>int main(int argc, char *argv[])
>{
> long int a;
> errno = 0;
> if ((a = strtol(argv[1], NULL, 16)) == -1)
According to your follow-up message, argv[1] is a string consisting of
15 f's. Under what conditions do you think this would ever produce a
value of -1? (You should read the description of strtol and determine
the only situation when it can produce a negative value.)
What is the size of long int on your system? How did you determine
this?
> printf("%s\n", strerror(errno));
What makes you think errno was set by anything your program did?
> printf("%d\n", a);
This undefined behavior has been addressed by others.
> return 0;
>}
>
--
Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2014-04-26 20:23 -0400 |
| Message-ID | <ljhin0$ldl$1@dont-email.me> |
| In reply to | #43623 |
"Barry Schwarz" <schwarzb@dqel.com> wrote in message
news:dsgol9h34hqo6q4jk00fl6boj6mneb6n4v@4ax.com...
> On Sat, 26 Apr 2014 16:51:17 -0400, "Bill Cunningham"
> <nospam@nspam.invalid> wrote:
>
>> My little utility doesn't print an errno value like it should. I have
>
> Nowhere in your code do you attempt to print the value of errno.
>
>>tried to fix it but have failed. I've never really used errno.h but always
>>perror() so what am I doing wrong here.
>>
>>#include <stdio.h>
>>#include <stdlib.h>
>>#include <errno.h>
>>#include <string.h>
>>
>>int main(int argc, char *argv[])
>>{
>> long int a;
>> errno = 0;
>> if ((a = strtol(argv[1], NULL, 16)) == -1)
>
> According to your follow-up message, argv[1] is a string consisting of
> 15 f's. Under what conditions do you think this would ever produce a
> value of -1? (You should read the description of strtol and determine
> the only situation when it can produce a negative value.)
>
> What is the size of long int on your system? How did you determine
> this?
>
>> printf("%s\n", strerror(errno));
>
> What makes you think errno was set by anything your program did?
>
>> printf("%d\n", a);
>
> This undefined behavior has been addressed by others.
>
>> return 0;
>>}
Works fine now. I've never used strtol or strtoll. I have occasssionally
used strtod and had great success. errno seems to set to ERANGE now when I
intentionally try to over or underflow. I am in the habit of -1 as in most
*nix sys calls being an error. I admit I should've look closer at the man
page.
Bill
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2014-04-26 20:27 -0400 |
| Message-ID | <ljhisn$m6t$1@dont-email.me> |
| In reply to | #43624 |
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main(int argc, char *argv[])
{
long int a;
errno = 0;
if ((a = strtol(argv[1], NULL, 16)) != 0)
printf("%s\n", strerror(errno));
printf("%ld\n", a);
return 0;
}
Works as it appears anyway. Who knows what subtle bug might be lurking.
[toc] | [prev] | [next] | [standalone]
| From | "Bill Cunningham" <nospam@nspam.invalid> |
|---|---|
| Date | 2014-04-26 20:42 -0400 |
| Message-ID | <ljhjpq$qop$1@dont-email.me> |
| In reply to | #43623 |
"Barry Schwarz" <schwarzb@dqel.com> wrote in message > What is the size of long int on your system? [...] Oh yeah ok sorry. Looks like a long and long long are 64 bits while an int is 32 bit. Bill
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web