473,480 Members | 5,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

itoa, atoi - confused

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
Nov 15 '05 #1
11 3571
Me again - what happened to getch etc ... I feel like everything I once knew
has gone!
"rayw" <ra*********@gmail.com> wrote in message
news:dh**********@news.ox.ac.uk...
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

Nov 15 '05 #2
rayw <ra*********@gmail.com> 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
er******@student.uu.se
Nov 15 '05 #3
rayw <ra*********@gmail.com> 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" <ra*********@gmail.com> wrote in message
news:dh**********@news.ox.ac.uk...
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
er******@student.uu.se
Nov 15 '05 #4
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" <ra*********@gmail.com> wrote in message
news:dh**********@news.ox.ac.uk...
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.
Nov 15 '05 #5

"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:1r************@brenda.flash-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

Nov 15 '05 #6

"rayw" <ra*********@gmail.com> wrote in message
news:dh**********@news.ox.ac.uk...
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
Nov 15 '05 #7
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.
Nov 15 '05 #8
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.
Nov 15 '05 #9

"rayw" <ra*********@gmail.com> 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.
Nov 15 '05 #10
"Malcolm" <re*******@btinternet.com> writes:
"rayw" <ra*********@gmail.com> 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
Nov 15 '05 #11
Ben Pfaff <bl*@cs.stanford.edu> writes:
"Malcolm" <re*******@btinternet.com> writes:
"rayw" <ra*********@gmail.com> 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) ks***@mib.org <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.
Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3673
by: John Lenton | last post by:
Is there any reason python's printf-style formatting is missing the (C99) '%a' specifier? I'm sorry if this has been asked and answered before; I can't find it on google ('%a' is a very awkward...
19
7417
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
7
12759
by: news.hku.hk | last post by:
Excuse me, i write the following function to add comma for integers but the unix server said: In function `class string comma(int)': implicit declaration of function `int itoa(...)'...
2
5005
by: Raskolnikow | last post by:
Hi! I have a very simple problem with itoa() or the localtime(...). Sorry, if it is too simple, I don't have a proper example. Please have a look at the comments. struct tm *systime; time_t...
2
6653
by: Sona | last post by:
Hi, I have a char* that holds an ascii character in its first element (at least I think that's what it holds because when I print it, it prints weird characters). I need to convert this into an...
29
20861
by: pete | last post by:
I wrote a version of itoa yesterday. Features: 1 No implementation defined arithmetic. All of the division and modulus division is done on positive values only. 2 No object is assumed...
10
20562
by: cai_rongxi | last post by:
I know how to write c code to realize atoi function, but I don't know how to do the opposit. Any sample code is appreciated.
24
8156
by: Mark | last post by:
hi, all i want is a simple function that takes an int, and returns a char* so i tried char * itoa(int i) { char buff; return _itoa(i,buff,10); }
7
39505
by: silverburgh.meryl | last post by:
Hi, Can you please tell me where I can find itoa()? I try to compile the following example, but I get the following error: .../t.cpp:20:2: warning: no newline at end of file .../t.cpp: In...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.