itoa, atoi - confused

R

rayw

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
 
R

rayw

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

Flash Gordon

rayw said:
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?

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.
 
E

Erik Trulsson

rayw said:
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.
 
E

Erik Trulsson

rayw said:
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.
 
R

rayw

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
 
M

Mike Wahler

rayw said:
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
 
M

Martin Ambuhl

rayw said:
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.
 
M

Martin Ambuhl

rayw said:
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.
 
M

Malcolm

rayw said:
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.
 
B

Ben Pfaff

Malcolm said:
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".
 
K

Keith Thompson

Ben Pfaff said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top