what is atoi( )

A

Arndt Jonasson

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

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

Ben Bacarisse

Ben Bacarisse 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?

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

pete

Ben said:
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.
 
P

pete

John said:
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.
 
D

David R Tribble

pete said:
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
 
C

Chris Smith

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

pete

Jordan said:
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/usr/src/libc/gen/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;
}
 

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