473,602 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ 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 3586
Me again - what happened to getch etc ... I feel like everything I once knew
has gone!
"rayw" <ra*********@gm ail.com> wrote in message
news:dh******** **@news.ox.ac.u k...
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*********@gm ail.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******@studen t.uu.se
Nov 15 '05 #3
rayw <ra*********@gm ail.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*********@gm ail.com> wrote in message
news:dh******** **@news.ox.ac.u k...
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******@studen t.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*********@gm ail.com> wrote in message
news:dh******** **@news.ox.ac.u k...
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.fla sh-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*********@gm ail.com> wrote in message
news:dh******** **@news.ox.ac.u k...
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*********@gm ail.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

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

Similar topics

11
3690
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 thing to look for): -- John Lenton (john@grulic.org.ar) -- Random fortune: Los cementerios están llenos de valientes.
19
7434
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()" for more information. > > **************************************************************** > Personal firewall software may warn about the connection IDLE > makes to its subprocess using this computer's internal loopback
7
12811
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(...)' ________________________________ string comma(int a){ char to_string; string s_a = itoa(a, to_string, 10);
2
5020
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 currentTime; char day; char month;
2
6669
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 integer and so I thought the following might work: char *somevar ... // somevar holds a value in somevar and is null terminated i.e. somevar = '\0'
29
20888
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 capable of representing the magnitude of INT_MIN. 3 The string is constructed in place without reversal. 4 No features from standard library are used.
10
20577
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
8179
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
39530
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 function 'int main()': .../t.cpp:13: error: 'itoa' was not declared in this scope /* itoa example */
0
8401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8054
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6730
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.