443,974 Members | 1,913 Online
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

# what is atoi( )

 P: n/a Hi could you please explain wat atoi( ) function is for and an example how to use it? Mar 3 '06 #1
47 Replies

 P: n/a sudharsan wrote: 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. Mar 3 '06 #2

 P: n/a > 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 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 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. Mar 3 '06 #3

 P: n/a sudharsan wrote: 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 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 . The atoi, atol, and atoll functions return the converted value. Example (mine, not from Standard): /* I haven't tested this */ #include int main(void) { return atoi("42"); } Mar 3 '06 #4

 P: n/a sudharsan wrote: 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. Mar 3 '06 #5

 P: n/a Jaspreet wrote: sudharsan wrote: 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. 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. Mar 3 '06 #6

 P: n/a Sunil Varma wrote: sudharsan wrote: Hi could you please explain wat atoi( ) function is for and an example how to use it? Usage : int atoi( const char *string ); No, this is declaration. Usage would be... see my post elsethread. Mar 3 '06 #7

 P: n/a "Vladimir S. Oka" 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. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Mar 3 '06 #8

 P: n/a "Jaspreet" writes: [...] We also have itoa() and other similar functions. There is no itoa() function in standard C. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Mar 3 '06 #9

 P: n/a thank you all for quick reply sudharsan wrote: Hi could you please explain wat atoi( ) function is for and an example how to use it? Mar 3 '06 #10

 P: n/a "sudharsan" 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_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Mar 3 '06 #11

 P: n/a Keith Thompson ha scritto: "Vladimir S. Oka" 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_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). -- 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

 P: n/a Keith Thompson wrote: "Vladimir S. Oka" 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_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Mar 3 '06 #13

 P: n/a David Paleino wrote: Keith Thompson ha scritto: "Vladimir S. Oka" 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_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. Mar 3 '06 #14

 P: n/a 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 :°) (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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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(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. -- pete Mar 3 '06 #19

 P: n/a 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(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. -- pete Mar 3 '06 #20

 P: n/a 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

 P: n/a 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

 P: n/a "Vladimir S. Oka" writes: 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.) Mar 3 '06 #23

 P: n/a Arndt Jonasson wrote: "Vladimir S. Oka" writes: 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? -- BR, Vladimir In 1869 the waffle iron was invented for people who had wrinkled waffles. Mar 3 '06 #24

 P: n/a 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

 P: n/a pete wrote: pete wrote:Richard Heathfield wrote:Ron Lima said: If the string cannot be converted to a number, at all, atoireturns 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

 P: n/a David Paleino 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(). 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. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> We must do something. This is something. Therefore, we must do this. Mar 3 '06 #27

 P: n/a Keith Thompson ha scritto: ... 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. 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

 P: n/a On Fri, 03 Mar 2006 12:22:32 +0100, in comp.lang.c , David Paleino 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 singledefinition 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 -- "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

 P: n/a Mark McIntyre ha scritto: On Fri, 03 Mar 2006 12:22:32 +0100, in comp.lang.c , David Paleino 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 singledefinition 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. 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

 P: n/a David Paleino wrote: Mark McIntyre ha scritto: On Fri, 03 Mar 2006 12:22:32 +0100, in comp.lang.c , David Paleino 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 singledefinition 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. 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; } -- pete Mar 3 '06 #31

 P: n/a Richard Heathfield writes: 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". 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")? :-) Mar 3 '06 #32

 P: n/a Micah Cowan wrote: .... 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: Also see Mar 4 '06 #33

 P: n/a sudharsan wrote: 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: Also see Mar 4 '06 #34

 P: n/a CBFalconer wrote (in article <44***************@yahoo.com>): sudharsan wrote: 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. By far the best answer in this thread. -- Randy Howard (2reply remove FOOBAR) "The power of accurate observation is called cynicism by those who have not got it." - George Bernard Shaw Mar 4 '06 #35

 P: n/a On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote: sudharsan wrote: 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. 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. -- Ben. Mar 5 '06 #36

 P: n/a Ben Bacarisse wrote (in article ): On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote: sudharsan wrote: 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. 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? -- Randy Howard (2reply remove FOOBAR) "The power of accurate observation is called cynicism by those who have not got it." - George Bernard Shaw Mar 5 '06 #37

 P: n/a On Sun, 05 Mar 2006 19:09:47 GMT, in comp.lang.c , Randy Howard wrote: Okay, I have a function that can be used safely in a few narrow,constricted input cases. I have another function that can beused 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 -- "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 5 '06 #38

 P: n/a Mark McIntyre wrote (in article <1c********************************@4ax.com>): On Sun, 05 Mar 2006 19:09:47 GMT, in comp.lang.c , Randy Howard 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? 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. -- Randy Howard (2reply remove FOOBAR) "The power of accurate observation is called cynicism by those who have not got it." - George Bernard Shaw Mar 5 '06 #39

 P: n/a On Sun, 05 Mar 2006 19:09:47 +0000, Randy Howard wrote: Ben Bacarisse wrote (in article ): On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote: sudharsan wrote: 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. 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? 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. -- Ben. Mar 6 '06 #40

 P: n/a Ben Bacarisse wrote: On Sun, 05 Mar 2006 19:09:47 +0000, Randy Howard wrote: Ben Bacarisse wrote (in article ): On Fri, 03 Mar 2006 23:00:15 -0500, CBFalconer wrote: sudharsan wrote:>> 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. 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? 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. -- pete Mar 6 '06 #41

 P: n/a John Smith wrote: 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." 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. -- pete Mar 6 '06 #42

 P: n/a pete wrote: 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 Mar 6 '06 #43

 P: n/a "Vladimir S. Oka" writes: Arndt Jonasson wrote: "Vladimir S. Oka" writes: 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. Mar 8 '06 #44

 P: n/a CBFalconer writes: sudharsan wrote: 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. 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. Mar 8 '06 #45

 P: n/a Vladimir S. Oka wrote: 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 Mar 10 '06 #46

 P: n/a On 2006-03-03 06:02:53 -0500, David Paleino

 P: n/a Jordan Abel wrote: > On 2006-03-03 06:02:53 -0500, David Paleino #include 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; } -- pete Oct 2 '06 #48

### This discussion thread is closed

Replies have been disabled for this discussion.