Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > atoi return

Reply
Thread Tools

atoi return

 
 
Bill Cunningham
Guest
Posts: n/a
 
      10-10-2008
I have just read atoi() returns no errors. It returns an int though and
the value of the int is supposed to be the value of the conversion. It seems
to me that right there tells you if there was success or not. Am I wrong?

Bill


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-10-2008
Bill Cunningham wrote:
> I have just read atoi() returns no errors. It returns an int though and
> the value of the int is supposed to be the value of the conversion. It seems
> to me that right there tells you if there was success or not. Am I wrong?
>

Wasn't this all beaten to death on one of your threads a week or two back?

What value would indicate failure?

--
Ian Collins
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      10-10-2008

"Bill Cunningham" <> writes:

> I have just read atoi() returns no errors. It returns an int though and
> the value of the int is supposed to be the value of the conversion. It seems
> to me that right there tells you if there was success or not. Am I wrong?
>
> Bill


What you should do is use sprintf and scanf and then do a string compare
with their output to your atoi output. If the result is the same then
you are on the right track.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-10-2008
Richard wrote:
> "Bill Cunningham" <> writes:
>
>> I have just read atoi() returns no errors. It returns an int though and
>> the value of the int is supposed to be the value of the conversion. It seems
>> to me that right there tells you if there was success or not. Am I wrong?
>>
>> Bill

>
> What you should do is use sprintf and scanf and then do a string compare
> with their output to your atoi output. If the result is the same then
> you are on the right track.
>

Are you baiting pool Bill, or posting *******s?

--
Ian Collins
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-10-2008
"Bill Cunningham" <> writes:
> I have just read atoi() returns no errors. It returns an int though and
> the value of the int is supposed to be the value of the conversion. It seems
> to me that right there tells you if there was success or not. Am I wrong?


Yes.

Take a look at the following program and tell me how you'd detect an
error in a call to atoi().

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *good = "0";
char *bad = "bad";
printf("atoi(\"%s\") = %d\n", good, atoi(good));
printf("atoi(\"%s\") = %d\n", bad, atoi(bad));
return 0;
}

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-10-2008

"Keith Thompson" <kst-> wrote in message
news:...


> Yes.
>
> Take a look at the following program and tell me how you'd detect an
> error in a call to atoi().
>
> #include <stdio.h>
> #include <stdlib.h>
> int main(void)
> {
> char *good = "0";
> char *bad = "bad";
> printf("atoi(\"%s\") = %d\n", good, atoi(good));
> printf("atoi(\"%s\") = %d\n", bad, atoi(bad));
> return 0;
> }


I can't really read that Keith but if I remember right this was
mentioned before but never really explained. I would've tried something
like this if it's even valid.
....
int i;
if (i=atoi(argv[1]))!=sizeof(int))
fprintf(stderr,"error\n");

*sigh* I dunno. Does this make any sense?

Bill



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-10-2008
"Bill Cunningham" <> writes:
> "Keith Thompson" <kst-> wrote in message
> news:...
>> Take a look at the following program and tell me how you'd detect an
>> error in a call to atoi().
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> int main(void)
>> {
>> char *good = "0";
>> char *bad = "bad";
>> printf("atoi(\"%s\") = %d\n", good, atoi(good));
>> printf("atoi(\"%s\") = %d\n", bad, atoi(bad));
>> return 0;
>> }

>
> I can't really read that Keith but if I remember right this was
> mentioned before but never really explained. I would've tried something
> like this if it's even valid.
> ...
> int i;
> if (i=atoi(argv[1]))!=sizeof(int))
> fprintf(stderr,"error\n");
>
> *sigh* I dunno. Does this make any sense?


No, it doesn't. What does sizeof(int) have to do with anything?

Yes, this was mentioned before, and it was explained in great detail.

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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      10-11-2008

"Keith Thompson" <kst-> wrote in message
news:...

[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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-11-2008
"Bill Cunningham" <> writes:
> "Keith Thompson" <kst-> wrote in message
> news:...
>
> [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().


Pretty much. It's not nearly as dangerous as gets, though; since its
behavior depends on the argument you pass to it (which you can check),
not on what appears on stdin (over which you typically have no
control), it *can* be used safely. But by the time you add code to
verify the argument before calling atoi(), you might as well call
strtol().

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-11-2008
Bill Cunningham wrote:
> "Keith Thompson" <kst-> wrote in message
>

.... snip ...
>
>> 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().


No. gets() is just unsafe regardless. atoi() can be used safely,
but why bother when you have strtol available.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Return Value of atoi and strtoul sachinahuja82@gmail.com C Programming 4 04-17-2007 06:46 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
atoi: stringstream or old C sprintf function Mike Chirico C++ 2 11-19-2003 03:59 PM
java equivalent function of c atoi() function lonelyplanet999 Java 8 11-03-2003 01:07 AM
int to char[]: What's the reciprocal of std::atoi? Magig Boatman C++ 1 07-11-2003 07:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57