what is atoi( )

R

Richard Heathfield

Vladimir S. Oka said:
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".
 
A

Arndt Jonasson

Vladimir S. Oka said:
David said:
[...]
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.)
 
V

Vladimir S. Oka

Arndt said:
Vladimir S. Oka said:
David said:
[...]
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?
 
D

David Paleino

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
 
J

John Smith

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

Keith Thompson

David Paleino said:
Vladimir S. Oka ha scritto:

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

David Paleino

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

Mark McIntyre

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
 
D

David Paleino

Mark McIntyre ha scritto:
You really don't need to.




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

pete

David said:
Mark McIntyre ha scritto:

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;
}
 
M

Micah Cowan

Richard Heathfield said:
Vladimir S. Oka said:


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")? :)
 
C

CBFalconer

sudharsan said:
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/>
 
C

CBFalconer

Micah said:
.... 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/>
 
R

Randy Howard

CBFalconer wrote
(in article said:
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.
 
B

Ben Bacarisse

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

Randy Howard

Ben Bacarisse wrote
(in article said:
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?
 
M

Mark McIntyre

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
 
R

Randy Howard

Mark McIntyre wrote
(in article said:
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.
 
A

Arndt Jonasson

Vladimir S. Oka said:
Arndt said:
Vladimir S. Oka said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top