Re: what should atoi() do with garbage input?
From: Simon Biber (news_at_ralminNOSPAM.cc)
Date: 11/10/03
- Next message: Joona I Palaste: "Re: Byte Alignment"
- Previous message: Shashi: "Byte Alignment"
- In reply to: Steve Gallagher: "what should atoi() do with garbage input?"
- Next in thread: Richard Bos: "Re: what should atoi() do with garbage input?"
- Reply: Richard Bos: "Re: what should atoi() do with garbage input?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 11 Nov 2003 07:48:46 +1100
"Steve Gallagher" <jeng_steveg@hotmail.com> wrote:
> Can someone tell me (hopefully with a pointer to the
> appropriate place in the Standard), what atoi() ought
> to do with the following:
>
>
> int outp = 0;
>
> outp = atoi("2+*&^%$#@!");
>
>
> One school of thought on my team is that atoi() is
> supposed to "grab the '2', convert it to 2, and basically
> just toss all that '+*&^%$#@!' stuff." Another school of
> thought is that atoi() should fail because the entire
> input was not convertiable to an int. Thanks in advance
> for any assistance.
C99 7.20.1.2#2
Except for the behavior on error, they are equivalent
to
atoi: (int)strtol(nptr, (char **)NULL, 10)
C99 7.20.1.4#2
First, they decompose the input string into three
parts: an initial, possibly empty, sequence of
white-space characters (as specified by the isspace
function), a subject sequence resembling an integer
represented in some radix determined by the value of
base, and a final string of one or more unrecognized
characters, including the terminating null character of
the input string. Then, they attempt to convert the
subject sequence to an integer, and return the result.
So, it should separate the input string "2+*&^%$#@!" into:
initial: ""
subject: "2"
final: "+*&^%$#@!"
It should convert the subject, and ignore the rest of the
string.
-- Simon.
- Next message: Joona I Palaste: "Re: Byte Alignment"
- Previous message: Shashi: "Byte Alignment"
- In reply to: Steve Gallagher: "what should atoi() do with garbage input?"
- Next in thread: Richard Bos: "Re: what should atoi() do with garbage input?"
- Reply: Richard Bos: "Re: what should atoi() do with garbage input?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|