Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > itoa, atoi - confused

Reply
Thread Tools

itoa, atoi - confused

 
 
rayw
Guest
Posts: n/a
 
      09-26-2005
I'm pretty new to C, although I did do some years ago now.

I've been told that itoa is no longer a standard function, and that the
ato... functions - although in the std - are not recommended.

So, I was wondering what was wrong with both itoa and atoi etc (and what's
replaced them).

Many thanks

ray


 
Reply With Quote
 
 
 
 
rayw
Guest
Posts: n/a
 
      09-26-2005
Me again - what happened to getch etc ... I feel like everything I once knew
has gone!


"rayw" <(E-Mail Removed)> wrote in message
news:dh8gr3$t7t$(E-Mail Removed)...
> I'm pretty new to C, although I did do some years ago now.
>
> I've been told that itoa is no longer a standard function, and that the
> ato... functions - although in the std - are not recommended.
>
> So, I was wondering what was wrong with both itoa and atoi etc (and what's
> replaced them).
>
> Many thanks
>
> ray
>



 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      09-26-2005
rayw wrote:
> Me again - what happened to getch etc ... I feel like everything I once knew
> has gone!


Please don't top post even when replying to yourself. Replies belong
*under* the portions of text you are replying to, not above them.

A: Because it means everything is backwards
Q: Why is top posting a right pain in the rear end?

> "rayw" <(E-Mail Removed)> wrote in message
> news:dh8gr3$t7t$(E-Mail Removed)...
>
>>I'm pretty new to C, although I did do some years ago now.
>>
>>I've been told that itoa is no longer a standard function, and that the
>>ato... functions - although in the std - are not recommended.
>>
>>So, I was wondering what was wrong with both itoa and atoi etc (and what's
>>replaced them).
>>
>>Many thanks


itoa was never in the standard. It might have been in whatever
implementation you used, but that does not make it standard.

atoi and friends are bad because:
1) The behaviour is undefined if the result does not fit in the target
type
2) They don't allow for detecting failure (did the user enter 0 or an
invalid string?)

> Me again - what happened to getch etc ...


getch was never in the standard, although it might have been in the
implementation you used.

> I feel like everything I once knew
> has gone!


The problem is that you learnt a load of vendor specific extensions
without learning that they are *not* part of the C language. Had you
learnt standard C you would not have this problem. So I suggest you
learn standard C now.

Extensions are not intrinsically bad (you can't write a GUI application
without them) but you should only use them knowingly and preferably in
isolated modules within your application.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Erik Trulsson
Guest
Posts: n/a
 
      09-26-2005
rayw <(E-Mail Removed)> wrote:
> I'm pretty new to C, although I did do some years ago now.
>
> I've been told that itoa is no longer a standard function, and that the
> ato... functions - although in the std - are not recommended.


itoa has *never* been a standard function, and the atoi family of fuctions
are indeed not recommended to use.

>
> So, I was wondering what was wrong with both itoa and atoi etc (and what's
> replaced them).


What is wrong with itoa is primarily that it is not standardized and therefore
completely unportable. The problem with atoi (etc.) is that you can't do
proper error checking if the number that is converted doesn't fit in the
desired type.

To convert strings to integers use strtol (etc.) instead, and to create a
string representation of an integer us sprintf.


--
<Insert your favourite quote here.>
Erik Trulsson
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Erik Trulsson
Guest
Posts: n/a
 
      09-26-2005
rayw <(E-Mail Removed)> wrote:
> Me again - what happened to getch etc ... I feel like everything I once knew
> has gone!
>


Just like itoa, getch has never been part of Standard C. If you used getch,
believing it was part of C, then much of what you thought you knew about C was
probably wrong.


>
> "rayw" <(E-Mail Removed)> wrote in message
> news:dh8gr3$t7t$(E-Mail Removed)...
>> I'm pretty new to C, although I did do some years ago now.
>>
>> I've been told that itoa is no longer a standard function, and that the
>> ato... functions - although in the std - are not recommended.
>>
>> So, I was wondering what was wrong with both itoa and atoi etc (and what's
>> replaced them).
>>
>> Many thanks
>>
>> ray
>>

>
>


--
<Insert your favourite quote here.>
Erik Trulsson
(E-Mail Removed)
 
Reply With Quote
 
rayw
Guest
Posts: n/a
 
      09-26-2005

"Flash Gordon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)-gordon.me.uk...

> itoa was never in the standard. It might have been in whatever
> implementation you used, but that does not make it standard.
>
> atoi and friends are bad because:
> 1) The behaviour is undefined if the result does not fit in the target
> type
> 2) They don't allow for detecting failure (did the user enter 0 or an
> invalid string?)


Thanks - clear now.

> The problem is that you learnt a load of vendor specific extensions
> without learning that they are *not* part of the C language. Had you
> learnt standard C you would not have this problem. So I suggest you learn
> standard C now.


Yup - it just might have been so long ago that it preceeded the stds
process. Amazed I can remember any of it.

r



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-26-2005

"rayw" <(E-Mail Removed)> wrote in message
news:dh8gr3$t7t$(E-Mail Removed)...
> I'm pretty new to C, although I did do some years ago now.
>
> I've been told that itoa is no longer a standard function,


It never was.

> and that the ato... functions - although in the std - are not recommended.
>
> So, I was wondering what was wrong with both itoa and atoi etc (and what's
> replaced them).


There's nothing inherently 'wrong' with 'itoa()', it's just
not part of standard C, thus not topical here.

The trouble with 'atoi()' is that there's no way to prevent
overflow (which gives undefined behavior) if the result can't be
represented by type 'int'. What is recommended in its place
is 'strtol()' which does have defined behavior in such a case.

-Mike


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-26-2005
rayw wrote:
> I'm pretty new to C, although I did do some years ago now.
>
> I've been told that itoa is no longer a standard function,


It was *never* a standard function.

> and that the
> ato... functions - although in the std - are not recommended.
>
> So, I was wondering what was wrong with both itoa


It need not exist since it has no specification in C.
If it does exist, it need not do what you think it does.

> and atoi etc


lack of error checking, among other things

>(and what's
> replaced them).


nothing replaced itoa: it never existed as part of the standard
libraries. You can always use sprintf().

The strto* functions do the work of atoi better.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-26-2005
rayw wrote:
> Me again - what happened to getch etc ... I feel like everything I once knew
> has gone!


getch() was never part of C. getc(), fgetc(), and getchar() are.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      09-27-2005

"rayw" <(E-Mail Removed)> wrote
>
> So, I was wondering what was wrong with both itoa and atoi etc (and what's
> replaced them).
>

The name isn't very logical, since it implies "integer to ASCII" and C
doesn't guarantee ASCII will be the execution set.

itoa duplicates the functionality of sprintf(), which is presumably why it
was never inclued in the official list of standard functions. atoi breaks
down when passed a legal integer that is too big to fit in an int, or when
passed a non-integer. strtol() is the replacement.


 
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
Re: atoi - please help Neil Vice C++ 0 04-01-2004 07:55 AM
atoi() Christopher Benson-Manica C++ 16 01-18-2004 11:28 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