Revision as of 18:59, 6 September 2011 editPrashantgonarkar (talk | contribs)83 edits →Function Code← Previous edit |
Latest revision as of 14:34, 11 June 2017 edit undoTom.Reding (talk | contribs)Autopatrolled, Extended confirmed users, Page movers, Template editors3,876,056 editsm +{{Redirect category shell}} for multiple-{{R}} #Rs using AWB |
(15 intermediate revisions by 9 users not shown) |
Line 1: |
Line 1: |
|
|
#REDIRECT ] |
|
{{lowercase|title=atoi}} |
|
|
'''atoi''' is a function in the ] that converts a string into an integer numerical representation. <code>atoi</code> stands for ''] to integer''. It is included in the ] ] <code>]</code>. Its prototype is as follows: |
|
|
|
|
|
|
|
{{Redirect category shell|1= |
|
<source lang="c"> |
|
|
|
{{R with history}} |
|
int atoi(const char *str); |
|
|
|
{{R to section}} |
|
</source> |
|
|
⚫ |
}} |
|
|
|
|
The <code>str</code> argument is a string, represented by an array of characters, containing the characters of a signed integer number. The string must be null-terminated. When atoi encounters a string with no numerical sequence, it returns zero (0). |
|
|
|
|
|
There are several variants of the '''atoi''' function, '''atol''', ''']''' and '''atoll''' , which are used to convert a string into a <code>long</code>, <code>double</code>, or <code>long</code> <code>long</code> type, respectively. The '''atoll''' was formerly known as '''atoq''' and was included into ]. |
|
|
==Function Code== |
|
|
|
|
|
/*atoi: convert s to integer*/ |
|
|
int atoi(char s) |
|
|
{ |
|
|
int i, n; |
|
|
|
|
|
n = 0; |
|
|
for(i = 0; s >= '0' && s <= '9'; i++) |
|
|
n = 10 * n + (s - '0') |
|
|
return n; |
|
⚫ |
} |
|
|
|
|
|
==Deficiencies== |
|
|
It is impossible to tell whether the string holds valid sequence of digits that represents the number 0 or invalid number as the function returns 0 in both cases. The newer function ] does not have this deficiency. |
|
|
|
|
|
'''atoi''' is neither ], nor ] on some operating systems.<ref name="codecogs_atoi">http://www.codecogs.com/reference/c/stdlib.h/atoi.php</ref> |
|
|
|
|
|
Also, '''atoi''' only converts base ten ascii values (this may also be a benefit depending on perspective). ] and other functions support alternate bases such as hexadecimal and octal. |
|
|
|
|
|
==Standards conformance== |
|
|
|
|
|
The '''atoi''', ], and '''atol''' functions are a part of the ISO standard C library (]), while the '''atoll''' function is added by ]. |
|
|
|
|
|
However, because of the ambiguity in returning 0 and lack of ]ty and ]ty on some operating systems, atoi is considered to be ] by ].<ref name="codecogs_atoi"/> |
|
|
|
|
|
==References== |
|
|
The © 1979 by Bell Telephone Laboratories, Incorporated. |
|
|
|
|
|
The written by ] (November 1971). |
|
|
|
|
|
<references/> |
|
|
|
|
|
|
|
|
==External links== |
|
|
|
|
|
* |
|
|
|
|
|
] |
|
|
] |
|
|
|
|
|
] |
|
|
] |
|
|
] |
|
|
] |
|