473,839 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is atoi( )

Hi
could you please explain wat atoi( ) function is for and an example
how to use it?

Mar 3 '06
47 46241
David Paleino wrote:

Mark McIntyre ha scritto:
On Fri, 03 Mar 2006 12:22:32 +0100, in comp.lang.c , David Paleino
<d.*******@gmai l.com> wrote:

Yes, I'm using gcc, the problem is that I can't really find it:

You really don't need to.

As you can see, at least in /usr/include, there isn't a single
definition for __strtol_intern al.

*shrug*. It could be internal to the compiler binary, or inside some
library, or whatever.
How gcc implements the fn is entirely up to it.


Sure, but it was just curiosity. I don't need it. No one ever will. ;)
It was just to understand how the atoi() function was implemented :D


a_toi does what atoi does, for valid arguments.

int a_toi(const char *nptr)
{
int n;

n = 0;
while (isspace(*nptr) ) {
++nptr;
}
if (*nptr != '-') {
if (*nptr == '+') {
++nptr;
}
while (isdigit(*nptr) ) {
n = 10 * n - '0' + *nptr++;
}
} else {
++nptr;
while (isdigit(*nptr) ) {
n = 10 * n + '0' - *nptr++;
}
}
return n;
}

--
pete
Mar 3 '06 #31
Richard Heathfield <in*****@invali d.invalid> writes:
Vladimir S. Oka said:

Richard Heathfield wrote:
Sunil Varma said:

> The return value is 0 if the input cannot be converted to a value of
> that type.

Chapter and verse please.


Is my reading of the Standrad correct in the sense that the error
return of `atoi` and friends is actually not specified (apart from
saying that it may differ from `strtoul`)?


4.10.1 String conversion functions

The functions atof , atoi , and atol need not affect the value of
the integer expression errno on an error. If the value of the result
cannot be represented, the behavior is undefined.
Now, how do you represent "the value of the result" if the call looks like
this?

int greeting = atoi("Hello, world!");

Whether this is an error would appear to depend on how "error" is defined
with respect to strtol - and the Standard doesn't even /use/ the word
"error" in the strtol section.

Having said that, the strtol section does explain that "Hello, world!" would
be parsed with an empty "subject sequence" (if the base is 10, as it would
be in this case), and 0 is returned.

So I guess it all depends on what you mean - or rather, what the Standard
means - by "error".


Well the only error condition actually defined for ato*() is in the
case that "the value of the result" (as I read it, of the implied
strto*()) cannot be represented. I would expect any implementation to
produce 0 for atoi("Hello, world!");. But I admit that the language is
a little shakey.

Now, what should I expect for atoi("100000000 000000000000000 00000")? :-)
Mar 3 '06 #32
Micah Cowan wrote:
.... snip ...
Now, what should I expect for atoi("100000000 000000000000000 00000")? :-)


If that value is greater than INT_MAX you get undefined (or
possibly implementation defined) behaviour. Look it up.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Mar 4 '06 #33
sudharsan wrote:

could you please explain wat atoi( ) function is for and an example
how to use it?


Its purpose is to confuse newbies and discourage proper testing for
input errors. Anyone with a modicum of knowledge would use
something in the strto*() family instead.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Mar 4 '06 #34
CBFalconer wrote
(in article <44************ ***@yahoo.com>) :
sudharsan wrote:

could you please explain wat atoi( ) function is for and an example
how to use it?


Its purpose is to confuse newbies and discourage proper testing for
input errors. Anyone with a modicum of knowledge would use
something in the strto*() family instead.


By far the best answer in this thread.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 4 '06 #35
On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote:
sudharsan wrote:

could you please explain wat atoi( ) function is for and an example how
to use it?


Its purpose is to confuse newbies and discourage proper testing for input
errors. Anyone with a modicum of knowledge would use something in the
strto*() family instead.


Maybe a little bit harsh. I agree with the "confusing newbies" part, but
there are cases like inside lex/flex rules where atoi is a reasonable
choice since you know what the string has in it.

--
Ben.
Mar 5 '06 #36
Ben Bacarisse wrote
(in article <pa************ *************** *@bsb.me.uk>):
On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote:
sudharsan wrote:

could you please explain wat atoi( ) function is for and an example how
to use it?


Its purpose is to confuse newbies and discourage proper testing for input
errors. Anyone with a modicum of knowledge would use something in the
strto*() family instead.


Maybe a little bit harsh. I agree with the "confusing newbies" part, but
there are cases like inside lex/flex rules where atoi is a reasonable
choice since you know what the string has in it.


Okay, I have a function that can be used safely in a few narrow,
constricted input cases. I have another function that can be
used in all cases. Why would I bother with the former?

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 5 '06 #37
On Sun, 05 Mar 2006 19:09:47 GMT, in comp.lang.c , Randy Howard
<ra*********@FO OverizonBAR.net > wrote:
Okay, I have a function that can be used safely in a few narrow,
constricted input cases. I have another function that can be
used in all cases. Why would I bother with the former?


int unsafe_conversi on(char*) { return do_stuff_fast() ;}

int safe_conversion (char*, int, int*) { return very_slow();}

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Mar 5 '06 #38
Mark McIntyre wrote
(in article <1c************ *************** *****@4ax.com>) :
On Sun, 05 Mar 2006 19:09:47 GMT, in comp.lang.c , Randy Howard
<ra*********@FO OverizonBAR.net > wrote:
Okay, I have a function that can be used safely in a few narrow,
constricted input cases. I have another function that can be
used in all cases. Why would I bother with the former?


int unsafe_conversi on(char*) { return do_stuff_fast() ;}

int safe_conversion (char*, int, int*) { return very_slow();}


I almost put in a "apart from performance reasons" in the
original, and now I wish I had. I'm sort of wondering whether
the "newbies" referenced originally care about that at all
though. They're probably more concerned with getting the right
answer on their homework.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 5 '06 #39
On Sun, 05 Mar 2006 19:09:47 +0000, Randy Howard wrote:
Ben Bacarisse wrote
(in article <pa************ *************** *@bsb.me.uk>):
On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote:
sudharsan wrote:

could you please explain wat atoi( ) function is for and an example
how to use it?

Its purpose is to confuse newbies and discourage proper testing for
input errors. Anyone with a modicum of knowledge would use something
in the strto*() family instead.


Maybe a little bit harsh. I agree with the "confusing newbies" part,
but there are cases like inside lex/flex rules where atoi is a
reasonable choice since you know what the string has in it.


Okay, I have a function that can be used safely in a few narrow,
constricted input cases. I have another function that can be used in all
cases. Why would I bother with the former?


A rhetorical question, presumably, since you know as well as I that there
is no reason for you to bother with any function whose behaviour is
covered by a more general one (if adequate performance is included in the
definition of a function's behaviour).

I gave a case where it was "reasonable " -- no more -- certainly not
preferable. I thought CBF's characterisatio n of its use as evidence of
not having "a modicum of knowledge" was rather harsh. That is all.

Its use in lex rules crops up in several articles by Mike Lesk. I would
not conclude from that that he lacks a modicum of knowledge. I do not
claim that as the author of lex and as a "name" his examples should be
seen as perfect, nor even that they would not be improved by replacing
atoi with strtoul/strtod, just that the use is reasonable and does not
demonstrate ignorance.

--
Ben.
Mar 6 '06 #40

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

Similar topics

19
7450
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
6
16056
by: John Smith | last post by:
What's wrong with the use of atoi in the following code? Why do I get the error message: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *' char cBuffer; void PushUnique(int); for(y = 0; y < 9; y++)
6
4271
by: Steve Gallagher | last post by:
Can someone tell me (hopefully with a pointer to the appropriate place in the Standard), what atoi() ought to do with the following: int outp = 0; outp = atoi("2+*&^%$#@!"); One school of thought on my team is that atoi() is supposed to
15
29222
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
2
2113
by: abcjayati | last post by:
hi! can you tell me what is a recrusive function? and what it does? and there are some lines in this small program that i could not understand (followed by???????) can u plz explain me those lines? plz //Program to implement a Recursive Function #include <iostream.h> #include <string.h> #include <stdlib.h> #include <conio.h> void recurfunc(int start,int stop,char* buff); void main() { clrscr();
7
2928
by: Giancarlo Bassi | last post by:
Please, what are here the 11 include files (found over the internet)? */mozzarella.c /* #include #include #include #include #include #include
11
4265
by: Nezhate | last post by:
Hi all, Can you help me? Why this warning appears in the next simple code ? warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast. #include <stdio.h> #include <stdlib.h> #include <string.h> int i;
50
5251
by: Bill Cunningham | last post by:
I have just read atoi() returns no errors. It returns an int though and the value of the int is supposed to be the value of the conversion. It seems to me that right there tells you if there was success or not. Am I wrong? Bill
10
4185
by: 66650755 | last post by:
First,thanks for all who have answered my last question. if char string="12345"; how could I convert the string(that is "3") to an int by using atoi? I only want to convert string,not other string. I've written these sentences: int a;
0
9696
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10903
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...
0
10584
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10290
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9425
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
7827
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
5865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4482
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
2
4063
muto222
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.