473,854 Members | 1,721 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
"sudharsan" <su*********@gm ail.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_Keit h) 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****@btopenw orld.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_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? (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****@btopenw orld.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_Keit h) 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****@btopenw orld.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_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.
(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_intern al *
inttypes.h:327: # ifndef __strtol_intern al_defined
inttypes.h:328: extern long int __strtol_intern al (__const char
*__restrict __nptr,
inttypes.h:331: # define __strtol_intern al_defined 1
inttypes.h:337: return __strtol_intern al (nptr, endptr, base, 0);
stdlib.h:286:#i fndef __strtol_intern al_defined
stdlib.h:287:ex tern long int __strtol_intern al (__const char *__restrict
__nptr,
stdlib.h:291:# define __strtol_intern al_defined 1
stdlib.h:333: return __strtol_intern al (__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_intern al.

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(npt r, (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(npt r, (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

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
11031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
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?
1
4563
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4162
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.