Misplaced Pages

Atoi: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 19:00, 6 September 2011 editPrashantgonarkar (talk | contribs)83 editsNo edit summary← Previous edit Revision as of 19:23, 6 September 2011 edit undoRichfife (talk | contribs)Extended confirmed users7,113 edits rv code addition. 1) Not appropriate. 2) Broken. Doesn't handle negative numbers.Next edit →
Line 9: Line 9:


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 ]. 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== ==Deficiencies==

Revision as of 19:23, 6 September 2011

atoi is a function in the C programming language that converts a string into an integer numerical representation. atoi stands for ASCII to integer. It is included in the C standard library header file stdlib.h. Its prototype is as follows:

int atoi(const char *str);

The str 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, atof and atoll , which are used to convert a string into a long, double, or long long type, respectively. The atoll was formerly known as atoq and was included into C99.

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 strtol does not have this deficiency.

atoi is neither thread-safe, nor async-cancel safe on some operating systems.

Also, atoi only converts base ten ascii values (this may also be a benefit depending on perspective). strtol and other functions support alternate bases such as hexadecimal and octal.

Standards conformance

The atoi, atof, and atol functions are a part of the ISO standard C library (C89), while the atoll function is added by C99.

However, because of the ambiguity in returning 0 and lack of thread-safety and async-cancel safety on some operating systems, atoi is considered to be deprecated by strtol.

References

The Version 7 Unix Manual Pages © 1979 by Bell Telephone Laboratories, Incorporated.

The Version 1 Unix Manual page for atoi written by Ken Thompson (November 1971).

  1. ^ http://www.codecogs.com/reference/c/stdlib.h/atoi.php


External links

Categories: