what should atoi() do with garbage input?

S

Steve Gallagher

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.
 
A

Andreas Kahari

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.

atoi() will grab the '2' but ignore the rest, just like strtol()
would do. It ignores any leading whitespace characters in the
strings, looks at what the standard calls "the subject sequence"
(everything that looks like a possibly signed floating point
number, or hex or one of NAN or INFINITY etc.) and converts it,
and ignores everything else. The current locale playes a role
as well.

It's in section 7.20.1.3.
 
S

Simon Biber

Steve Gallagher said:
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.
 
R

Richard Bos

C99 7.20.1.2#2
Except for the behavior on error, they are equivalent
to
atoi: (int)strtol(nptr, (char **)NULL, 10)

Also note that, because of the better behaviour on error referred to in
this clause, strtol() is superior to atoi(). You'd be better off not
using atoi() at all, but switching to strtol().

Richard
 
D

Dan Pop

In said:
atoi() will grab the '2' but ignore the rest, just like strtol()
would do. It ignores any leading whitespace characters in the
strings, looks at what the standard calls "the subject sequence"
(everything that looks like a possibly signed floating point
^^^^^^^^^^^^^^
number, or hex or one of NAN or INFINITY etc.) and converts it,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and ignores everything else. The current locale playes a role
as well.

Are you *sure* you know what you're talking about?

Dan
 
A

Andreas Kahari

In <[email protected]> Andreas Kahari


Are you *sure* you know what you're talking about?

I thought so, but I'm getting the feeling I'm now wrong. It's
only strtod() that knows about NAN and INFINITY, isn't it? I
haven't got the standard here at the moment.

Would a strtol() implementation that converts strings like
"1.2e3" into floating point numbers and then casts them to
integers ("1.2e3" --> 1200) violate the standard?
 
D

Dan Pop

In said:
I thought so, but I'm getting the feeling I'm now wrong. It's
only strtod() that knows about NAN and INFINITY, isn't it? I
haven't got the standard here at the moment.

Yup, strtod and its C99 friends, strtof and strtold.
Would a strtol() implementation that converts strings like
"1.2e3" into floating point numbers and then casts them to
integers ("1.2e3" --> 1200) violate the standard?

Most definitely. The right value (modulo locale-specific oddities) is
1 for "1.2e3", because the dot/decimal point is not part of the
subject sequence for an integer constant.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,099
Messages
2,570,626
Members
47,237
Latest member
David123

Latest Threads

Top