what is atoi( )

S

sudharsan

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

Jaspreet

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

Ron Lima

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

Vladimir S. Oka

sudharsan said:
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");
}
 
S

Sunil Varma

sudharsan said:
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.
 
V

Vladimir S. Oka

Jaspreet said:
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.
 
K

Keith Thompson

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

Keith Thompson

sudharsan said:
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?
 
V

Vladimir S. Oka

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

David Paleino

Keith Thompson ha scritto:
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).
 
V

Vladimir S. Oka

David said:
Keith Thompson ha scritto:

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

David Paleino

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 :°)
The best way to really understand that is to try and code it yourself.

Yeah, I know, I'll try asap.

Cheers,
David
 
V

Vladimir S. Oka

Richard said:
Sunil Varma said:


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

pete

Richard said:
Ron Lima said:


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

pete

pete said:
Richard said:
Ron Lima said:


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.
 

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,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top