"Steve Gallagher" <je*********@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.