itoa, atoi - confused

Discussion in 'C Programming' started by rayw, Sep 26, 2005.

  1. rayw

    rayw Guest

    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
    rayw, Sep 26, 2005
    #1
    1. Advertising

  2. rayw

    rayw Guest

    Me again - what happened to getch etc ... I feel like everything I once knew
    has gone!


    "rayw" <> wrote in message
    news:dh8gr3$t7t$...
    > 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
    >
    rayw, Sep 26, 2005
    #2
    1. Advertising

  3. rayw

    Flash Gordon Guest

    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" <> wrote in message
    > news:dh8gr3$t7t$...
    >
    >>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.
    Flash Gordon, Sep 26, 2005
    #3
  4. 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, 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
    Erik Trulsson, Sep 26, 2005
    #4
  5. rayw <> 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" <> wrote in message
    > news:dh8gr3$t7t$...
    >> 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
    Erik Trulsson, Sep 26, 2005
    #5
  6. rayw

    rayw Guest

    "Flash Gordon" <> wrote in message
    news:-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
    rayw, Sep 26, 2005
    #6
  7. rayw

    Mike Wahler Guest

    "rayw" <> wrote in message
    news:dh8gr3$t7t$...
    > 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
    Mike Wahler, Sep 26, 2005
    #7
  8. 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.
    Martin Ambuhl, Sep 26, 2005
    #8
  9. 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.
    Martin Ambuhl, Sep 26, 2005
    #9
  10. rayw

    Malcolm Guest

    "rayw" <> 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.
    Malcolm, Sep 27, 2005
    #10
  11. rayw

    Ben Pfaff Guest

    "Malcolm" <> writes:

    > "rayw" <> 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.


    Is that what the "a" stands for? I'd always assumed it was short
    for "array of character".
    --
    "Some people *are* arrogant, and others read the FAQ."
    --Chris Dollin
    Ben Pfaff, Sep 27, 2005
    #11
  12. Ben Pfaff <> writes:
    > "Malcolm" <> writes:
    >
    >> "rayw" <> 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.

    >
    > Is that what the "a" stands for? I'd always assumed it was short
    > for "array of character".


    Then "itos", for integer to string, would have made more sense.

    I suspect that when itoa and atoi were first written, there was a de
    facto guarantee of ASCII (though C was ported to EBCDIC systems fairly
    early on).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 27, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.hku.hk

    itoa problem?

    news.hku.hk, May 1, 2004, in forum: C++
    Replies:
    7
    Views:
    8,180
    Siemel Naran
    May 2, 2004
  2. Moritz Beller

    itoa function in gcc

    Moritz Beller, Aug 18, 2004, in forum: C++
    Replies:
    4
    Views:
    15,898
    Moritz Beller
    Aug 19, 2004
  3. jack
    Replies:
    3
    Views:
    5,031
  4. les sources de itoa

    , May 5, 2005, in forum: C++
    Replies:
    1
    Views:
    569
    red floyd
    May 5, 2005
  5. Sona

    Confusing itoa and atoi

    Sona, Sep 22, 2003, in forum: C Programming
    Replies:
    2
    Views:
    477
    Darrell Grainger
    Sep 22, 2003
Loading...

Share This Page