Re: atoi return




"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:ln1vyokqw1.fsf@xxxxxxxxxxxxxxxxxx

[snip]

Here's the point. If you give atoi() a string that doesn't represent
a number, such as the string "bad", it returns 0. If you give it the
string "0", which does represent a number it returns 0. If atoi()
returns 0, you *can't tell* whether it successfully converted the
string "0" or failed to convert the string "bad".

Even worse, if atoi() is given a string that represents a number
that's too big to hold in an int, it invokes undefined behavior. For
example, there's no telling what atoi("99999999999999999999") will do;
it could crash your program or worse.

I see. Must be a left over dinosaur like gets().

Bill


.



Relevant Pages

  • Re: Check all errors in code?
    ... The atoi function converts the initial portion of the string ... pointed to by nptr to int representation. ... The atoi function returns the converted value. ... The strtol function returns the converted value, ...
    (comp.lang.c)
  • Re: a question abou "atoi"
    ... atoi is dangerous; avoid it. ... A single character isn't a string ... Ignore what it says about converting to a pointer type; ...
    (comp.lang.c)
  • Re: atoi return
    ... that's too big to hold in an int, ... atoi() can be used safely, ... it's a left over dinosaur. ... is intended to convert a string value into an int value. ...
    (comp.lang.c)
  • Re: Using popen get extra characters(specially HP) when saving in file
    ... I realize a couple of days later about the error on funcion atoi. ... The method of the declarations of the arguments of function I use,is ... > the string you pass to the function), and for i equals 9 it's the ... > cases the second element of the 'ki' array is an unitialized value. ...
    (comp.unix.programmer)
  • Re: atol of a value > INT_MAX
    ... And the call to atoi still invokes undefined behavior. ... Since your struct contains a pointer, initializing it to all bits zero ... Is there a problem with keeping it as a string? ... Does you compiler support any integer type larger than long (or ...
    (comp.lang.c)