473,854 Members | 1,476 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 46252
Ben Bacarisse wrote:

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


Adequate performance isn't included in the
definition of any standard function's behaviour.

There are many standard functions whose behavior
is covered by more general ones.

I once advised a colleague
on how to shrink the size of his embedded program
by replacing all his fprintf calls, with fputs calls.

--
pete
Mar 6 '06 #41
John Smith wrote:

pete wrote:
pete wrote:
Richard Heathfield wrote:

Ron Lima said:
>If the string cannot be converted to a number, at all, atoi
>returns zero.

Chapter and verse, please.

It depends on whether "no conversion" is the same thing as "error".
Is atoi(""), an error?


from Harbison & Steele, p.411:

"If the functions in this section (i.e. atox() family) are unable
to convert the input string, then their behavior is undefined."


I think atoi("") is equal to zero.

The standard says this about atoi:
Except for
the behavior on error, they are equivalent to
atoi: (int)strtol(npt r, (char **)NULL, 10)

and this about strtol:
If no conversion could
be performed, zero is returned. If the correct value is
outside the range of representable values, LONG_MIN,
LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is
returned (according to the return type and sign of the
value, if any), and the value of the macro ERANGE is stored
in errno.

Those words make me think that attempting to convert
a string to an out of range integer, is an error,
and that attempting to convert a nonconvertable string,
is not an error.

--
pete
Mar 6 '06 #42
pete wrote:
I think atoi("") is equal to zero.

The standard says this about atoi:
Except for
the behavior on error, they are equivalent to
atoi: (int)strtol(npt r, (char **)NULL, 10)

and this about strtol:
If no conversion could
be performed, zero is returned. If the correct value is
outside the range of representable values, LONG_MIN,
LONG_MAX, LLONG_MIN, LLONG_MAX, ULONG_MAX, or ULLONG_MAX is
returned (according to the return type and sign of the
value, if any), and the value of the macro ERANGE is stored
in errno.

Those words make me think that attempting to convert
a string to an out of range integer, is an error,
and that attempting to convert a nonconvertable string,
is not an error.


Yes, which would also cover cases like atoi("xyz").

Note that an implementation is free to set errno to some nonzero
value as well (just don't count on it).

-drt

Mar 6 '06 #43

"Vladimir S. Oka" <no****@btopenw orld.com> writes:
Arndt Jonasson wrote:

"Vladimir S. Oka" <no****@btopenw orld.com> writes:
David Paleino wrote:
> [...]
> Now, looking into stdlib.h, I see that strtol() refers to
> __strtol_intern al(), but I can't find it:
>
> "
> extern __inline long int __NTH (strtol (__const char *__restrict
> __nptr, char **__restrict __endptr, int __base))
> {
> return __strtol_intern al (__nptr, __endptr, __base, 0);
> }
> "
>
> Does anyone have a slight idea where __strtol_intern al is placed?

It is placed in the standard library implementation. You may not have
access to the source code for it (you might if you're using gcc).
Even if you had, it's not guaranteed to be in C, or any other
language you can think of.


Does __strtol_intern al even have to be a function? Isn't it the case
that the compiler is allowed to do some appropriate optimization,
having complete knowledge about the semantics of the call? (For
example, partially unroll a loop - maybe 'strtol' isn't the best
candidate, though.)


C Standard really does not care how atoi and friends are implemented, as
long as they do as Standard requires. They might as well send carrier
pigeons to Egypt. I'm not familiar enough with any Standard C library
implementation to tell you how it's "usually" done. Anyone?


I didn't mean just any implementation of 'atoi', though I probably
didn't express myself clearly enough. I meant this particular one,
where a header file in the implementation contains what seems to be a
call to a function in the implementation' s reserved name space. I wondered
whether that function must actually exist as a function, or if this is
allowed to be, so to speak, private communication to the compiler, and
the actual implementation need not be expressible in standard C at all.
Mar 8 '06 #44

CBFalconer <cb********@yah oo.com> writes:
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.


My habit has been to use 'atoi' when I need to convert a command line
argument to a number, and the number must be larger than 0. Then the
code looks like

if ((x = atoi(arg)) == 0)
usage();

Using 'strtol', it seems I have to do:

errno = 0;
x = strtol(arg, NULL, 10);
if (errno != 0)
usage();

though I do get the advantage that overflow is detected. If 0 is
allowed input, using 'strtol' also needs a check whether the input
string is empty, since it treats "" as valid (why was this considered
a good idea?). I have usually used 'sscanf' in this case.

My quibble doesn't amount to much, since I can't recommend a function
that doesn't detect malformed input before one that does, and the portion
of a program that deals with command line arguments is usually very small
compared to the rest.
Mar 8 '06 #45
Vladimir S. Oka <no****@btopenw orld.com> wrote:
C Standard really does not care how atoi and friends are implemented, as
long as they do as Standard requires. They might as well send carrier
pigeons to Egypt. I'm not familiar enough with any Standard C library
implementation to tell you how it's "usually" done. Anyone?


It's generally the carrier pigeons, I think.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Mar 10 '06 #46
On 2006-03-03 06:02:53 -0500, David Paleino <d.*******@gmai l.comsaid:
Does anyone have a slight idea where __strtol_intern al is placed? (I
just want to understand how atoi() effectively works :P)
It's probably the same as strtol, but with an extra argument.

strtol is a bit more complex than atoi

here's an atoi function:
http://minnie.tuhs.org/UnixTree/V7/u...en/atoi.c.html

though it's unclear why it doesn't do switch(*p++) instead. compiler bug?

Oct 2 '06 #47
Jordan Abel wrote:
>
On 2006-03-03 06:02:53 -0500,
David Paleino <d.*******@gmai l.comsaid:
Does anyone have a slight idea where __strtol_intern al is placed? (I
just want to understand how atoi() effectively works :P)

It's probably the same as strtol, but with an extra argument.

strtol is a bit more complex than atoi

here's an atoi function:
http://minnie.tuhs.org/UnixTree/V7/u...en/atoi.c.html

though it's unclear why it doesn't do switch(*p++) instead.
compiler bug?
My version of atoi looks something like this:

#include <stdlib.h>
#include <ctype.h>

int atoi(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
Oct 2 '06 #48

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

Similar topics

19
7454
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
29223
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
5258
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
9752
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,...
1
10763
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
10371
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
9518
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
7918
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
7082
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5750
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
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3188
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.