473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

what is atoi( )

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

Mar 3 '06 #1
47 46081

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


Just pick up any book on C or do a man atoi to help you. It converts a
text to integer value.

for example atoi("234") would return you 234 as int on which you can
subsequently do arithmetic operations.

We also have itoa() and other similar functions.

char str[]= "234";

int iVal;

iVal = atoi(str);

It basically subtracts '0' from each character to get you the numeric.

Mar 3 '06 #2
> could you please explain wat atoi( ) function is for and an example
how to use it?


The atoi function tries to convert an string to a integer number. For
instance:

#include <stdlib.h>

int main (void) {
int a;

a = atoi ("1234");
return 0;
}

In this example, atoi will convert the string "1234" to the integer
number 1234. This is another example:

#include <stdlib.h>

int main (void) {
int a = atoi ("223asdf");
return 0;
}

This will convert "223" to 223. The remainder of the string is not
considered. If the string cannot be converted to a number, at all, atoi
returns zero.

Mar 3 '06 #3

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


You're much better of finding a good C book that covers the standard
library.

Here's excerpt from the C Standard (7.20.1.2p1-3):

#include <stdlib.h>
int atoi(const char *nptr);
long int atol(const char *nptr);
long long int atoll(const char *nptr);

The atoi, atol, and atoll functions convert the initial portion of the
string pointed to by nptr to int, long int, and long long int
representation, respectively. Except for the behavior on error, they
are equivalent to <examples of calls to strtol()>.

The atoi, atol, and atoll functions return the converted value.

Example (mine, not from Standard):

/* I haven't tested this */
#include <stdlib.h>

int main(void)
{
return atoi("42");
}

Mar 3 '06 #4

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

Usage :

int atoi( const char *string );

What it does:

atoi() returns the int value produced by interpreting the input
characters as a number.
That is the integer equivalent of the char string you pass as the
argument.

The return value is 0 if the input cannot be converted to a value of
that type. The return value is undefined in case of overflow.

Mar 3 '06 #5
Jaspreet wrote:
sudharsan wrote:
Hi
could you please explain wat atoi( ) function is for and an example
how to use it?
Just pick up any book on C or do a man atoi to help you. It converts a
text to integer value.


Not "text", but a string pointed to by the argument. There's no such
thing as "text" in C.
for example atoi("234") would return you 234 as int on which you can
subsequently do arithmetic operations.

We also have itoa() and other similar functions.

char str[]= "234";

int iVal;

iVal = atoi(str);

It basically subtracts '0' from each character to get you the numeric.


What a load of cobblers! Of course it does work that way, NOT!

If you decide to give advice, do try and give good, accurate advice.
BTW, exactly how `atoi` and friends are implemented is outside of scope
of C Standard, and off-topic here.

Mar 3 '06 #6

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

Usage :

int atoi( const char *string );


No, this is declaration. Usage would be... see my post elsethread.

Mar 3 '06 #7
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Jaspreet wrote:

[...]
for example atoi("234") would return you 234 as int on which you can
subsequently do arithmetic operations.

We also have itoa() and other similar functions.

char str[]= "234";

int iVal;

iVal = atoi(str);

It basically subtracts '0' from each character to get you the numeric.


What a load of cobblers! Of course it does work that way, NOT!


It could, and very likely does, subtract '0' from each character value
to get the numeric value of that digit. Of course that's not all it
does.

--
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.
Mar 3 '06 #8
"Jaspreet" <js***********@gmail.com> writes:
[...]
We also have itoa() and other similar functions.


There is no itoa() function in standard C.

--
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.
Mar 3 '06 #9
thank you all for quick reply
sudharsan wrote:
Hi
could you please explain wat atoi( ) function is for and an example
how to use it?


Mar 3 '06 #10
"sudharsan" <su*********@gmail.com> writes:
Hi
could you please explain wat atoi( ) function is for and an example
how to use it?


When you posted this same question in comp.std.c, I wrote:
] This is a language question, not a standard question, so it would be
] more appropriate in comp.lang.c.
]
] Check the index of your C textbook, or your system's documentation
] ("man atoi" should work on Unix-like systems), or do a Google search.

You followed by advice about posting to a more appropriate newsgroup,
but you seem to have ignored the rest of what I wrote. Did you check
your C textbook, or your system's documentation, or do a Google
search?

--
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.
Mar 3 '06 #11
Keith Thompson ha scritto:
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Jaspreet wrote:

...

It basically subtracts '0' from each character to get you the numeric.


What a load of cobblers! Of course it does work that way, NOT!

It could, and very likely does, subtract '0' from each character value
to get the numeric value of that digit. Of course that's not all it
does.


Well, doing a `man atoi', it explains the function this way:

"
....

int atoi(const char *nptr);

....

The atoi() function converts the initial portion of the string pointed
to by nptr to int. The behaviour is the same as

strtol(nptr, (char **)NULL, 10);

except that atoi() does not detect errors."

Now, looking into stdlib.h, I see that strtol() refers to
__strtol_internal(), but I can't find it:

"
extern __inline long int __NTH (strtol (__const char *__restrict __nptr,
char **__restrict __endptr, int __base))
{
return __strtol_internal (__nptr, __endptr, __base, 0);
}
"

Does anyone have a slight idea where __strtol_internal is placed? (I
just want to understand how atoi() effectively works :P)

David
P.S.: I'm using the stdlib.h found in /usr/include/, which I believe
being the standard stdlib.h (not sure though).

--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<
Mar 3 '06 #12

Keith Thompson wrote:
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Jaspreet wrote: [...]
for example atoi("234") would return you 234 as int on which you can
subsequently do arithmetic operations.

We also have itoa() and other similar functions.

char str[]= "234";

int iVal;

iVal = atoi(str);

It basically subtracts '0' from each character to get you the numeric.


What a load of cobblers! Of course it does work that way, NOT!


It could, and very likely does, subtract '0' from each character value
to get the numeric value of that digit. Of course that's not all it
does.


I know, but the manner of the sentence, combined with inexperience of
the OP, for me, spelled disaster sometime in not to distant future.
That's why I decided to try and nip it in the bud.


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


Mar 3 '06 #13

David Paleino wrote:
Keith Thompson ha scritto:
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Jaspreet wrote:

...

It basically subtracts '0' from each character to get you the numeric.

What a load of cobblers! Of course it does work that way, NOT!

It could, and very likely does, subtract '0' from each character value
to get the numeric value of that digit. Of course that's not all it
does.


Well, doing a `man atoi', it explains the function this way:

"
...

int atoi(const char *nptr);

...

The atoi() function converts the initial portion of the string pointed
to by nptr to int. The behaviour is the same as

strtol(nptr, (char **)NULL, 10);

except that atoi() does not detect errors."

Now, looking into stdlib.h, I see that strtol() refers to
__strtol_internal(), but I can't find it:

"
extern __inline long int __NTH (strtol (__const char *__restrict __nptr,
char **__restrict __endptr, int __base))
{
return __strtol_internal (__nptr, __endptr, __base, 0);
}
"

Does anyone have a slight idea where __strtol_internal 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.
(I just want to understand how atoi() effectively works :P)


The best way to really understand that is to try and code it yourself.

Mar 3 '06 #14
Vladimir S. Oka ha scritto:

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

Yes, I'm using gcc, the problem is that I can't really find it:
neo@matrix:/usr/include$ grep -inR __strtol_internal *
inttypes.h:327:# ifndef __strtol_internal_defined
inttypes.h:328:extern long int __strtol_internal (__const char
*__restrict __nptr,
inttypes.h:331:# define __strtol_internal_defined 1
inttypes.h:337: return __strtol_internal (nptr, endptr, base, 0);
stdlib.h:286:#ifndef __strtol_internal_defined
stdlib.h:287:extern long int __strtol_internal (__const char *__restrict
__nptr,
stdlib.h:291:# define __strtol_internal_defined 1
stdlib.h:333: return __strtol_internal (__nptr, __endptr, __base, 0);
neo@matrix:/usr/include$
As you can see, at least in /usr/include, there isn't a single
definition for __strtol_internal.

By the way: why shouldn't it be written in C? I don't really think that
they wrote the standard libraries in Assembler, or in Binary code :°)
(I just want to understand how atoi() effectively works :P)

The best way to really understand that is to try and code it yourself.


Yeah, I know, I'll try asap.

Cheers,
David

--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<
Mar 3 '06 #15
Ron Lima said:
If the string cannot be converted to a number, at all, atoi
returns zero.


Chapter and verse, please.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 3 '06 #16
Sunil Varma said:
The return value is 0 if the input cannot be converted to a value of
that type.


Chapter and verse please.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 3 '06 #17

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`)?

My guess is that most people use libraries that implement `atoi` using
`strtoul` (e.g. like in atoi.c in P.J. Plauger's book). If you do that,
you get what `strtoul` returns on error, and that's speicified as 0 by
the Standard.

Mar 3 '06 #18
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?

N869
7.20.1.2 The atoi, atol, and atoll functions
[#2] The atoi, atol, and atoll functions convert the initial
portion of the string pointed to by nptr to int, long int,
and long long int representation, respectively. Except for
the behavior on error, they are equivalent to
atoi: (int)strtol(nptr, (char **)NULL, 10)

7.20.1.4 The strtol, strtoll, strtoul, and strtoull
functions
[#7] If the subject sequence is empty or does not have the
expected form, no conversion is performed; the value of nptr
is stored in the object pointed to by endptr, provided that
endptr is not a null pointer.

--
pete
Mar 3 '06 #19
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?

N869
7.20.1.2 The atoi, atol, and atoll functions
[#2] The atoi, atol, and atoll functions convert the initial
portion of the string pointed to by nptr to int, long int,
and long long int representation, respectively. Except for
the behavior on error, they are equivalent to
atoi: (int)strtol(nptr, (char **)NULL, 10)

7.20.1.4 The strtol, strtoll, strtoul, and strtoull
functions
[#7] If the subject sequence is empty or does not have the
expected form, no conversion is performed; the value of nptr
is stored in the object pointed to by endptr, provided that
endptr is not a null pointer.


Wrong quote
[#8] The strtol, strtoll, strtoul, and strtoull functions
return the converted value, if any. 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.

--
pete
Mar 3 '06 #20
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".

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 3 '06 #21
sudharsan wrote:
could you please explain wat atoi( ) function is for and an example
how to use it?


It's something you can lookup with any search engine :

http://www.google.nl/search?hl=nl&q=...e+zoeken&meta=

*please* try to do some thinking on your own instead of asking these
kind of questions.

Igmar
Mar 3 '06 #22

"Vladimir S. Oka" <no****@btopenworld.com> writes:
David Paleino wrote:
[...]
Now, looking into stdlib.h, I see that strtol() refers to
__strtol_internal(), but I can't find it:

"
extern __inline long int __NTH (strtol (__const char *__restrict __nptr,
char **__restrict __endptr, int __base))
{
return __strtol_internal (__nptr, __endptr, __base, 0);
}
"

Does anyone have a slight idea where __strtol_internal 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_internal 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.)
Mar 3 '06 #23
Arndt Jonasson wrote:

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

--
BR, Vladimir

In 1869 the waffle iron was invented for people who had wrinkled
waffles.

Mar 3 '06 #24
Vladimir S. Oka ha scritto:

...

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?


Lol, not me (unfortunately).
Trying to look about gcc's implementation of atoi().

Cheers,
David

--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<
Mar 3 '06 #25
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."
Mar 3 '06 #26
David Paleino <d.*******@gmail.com> writes:
Vladimir S. Oka ha scritto:
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?


Lol, not me (unfortunately).
Trying to look about gcc's implementation of atoi().


<OT>
gcc doesn't implement atoi(). gcc is a compiler, not a complete C
implementation; it uses whatever C library is provided by the
operating system.
</OT>

--
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.
Mar 3 '06 #27
Keith Thompson ha scritto:
...

<OT>
gcc doesn't implement atoi(). gcc is a compiler, not a complete C
implementation; it uses whatever C library is provided by the
operating system.
</OT>


erm... yes, I know, probably I should have said "GNU's C implementation"?

Thanks

(sorry for the OT :P)

--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<
Mar 3 '06 #28
On Fri, 03 Mar 2006 12:22:32 +0100, in comp.lang.c , David Paleino
<d.*******@gmail.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_internal.


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

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 3 '06 #29
Mark McIntyre ha scritto:
On Fri, 03 Mar 2006 12:22:32 +0100, in comp.lang.c , David Paleino
<d.*******@gmail.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_internal.

*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
Mark McIntyre


David Paleino
--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<
Mar 3 '06 #30
David Paleino wrote:

Mark McIntyre ha scritto:
On Fri, 03 Mar 2006 12:22:32 +0100, in comp.lang.c , David Paleino
<d.*******@gmail.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_internal.

*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*****@invalid.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("10000000000000000000000000000")? :-)
Mar 3 '06 #32
Micah Cowan wrote:
.... snip ...
Now, what should I expect for atoi("10000000000000000000000000000")? :-)


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.com, 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/googlegroupsreply/>
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.com, 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/googlegroupsreply/>
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*********@FOOverizonBAR.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_conversion(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*********@FOOverizonBAR.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_conversion(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 characterisation 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
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(nptr, (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(nptr, (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****@btopenworld.com> writes:
Arndt Jonasson wrote:

"Vladimir S. Oka" <no****@btopenworld.com> writes:
David Paleino wrote:
> [...]
> Now, looking into stdlib.h, I see that strtol() refers to
> __strtol_internal(), but I can't find it:
>
> "
> extern __inline long int __NTH (strtol (__const char *__restrict
> __nptr, char **__restrict __endptr, int __base))
> {
> return __strtol_internal (__nptr, __endptr, __base, 0);
> }
> "
>
> Does anyone have a slight idea where __strtol_internal 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_internal 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********@yahoo.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****@btopenworld.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.*******@gmail.comsaid:
Does anyone have a slight idea where __strtol_internal 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.*******@gmail.comsaid:
Does anyone have a slight idea where __strtol_internal 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
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()"...
6
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...
6
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...
15
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
2
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?...
7
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
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...
50
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...
10
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.