473,854 Members | 1,665 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 46251
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****@btopenw orld.com> writes:
David Paleino wrote:
[...]
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.


Does __strtol_intern al 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****@btopenw orld.com> writes:
David Paleino wrote:
> [...]
> 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.


Does __strtol_intern al 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.*******@gmai l.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_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 #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.*******@gmai l.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_intern al.


*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.*******@gmai l.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_intern al.

*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

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
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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...
0
10684
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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
5744
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?
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.