Itoa

Itoa

The itoa function is a widespread non-standard extension to the standard C programming language. It cannot be portably used, as it is not defined in any of the C language standards; however, compilers often provide it through the header <stdlib.h> while in non-conforming mode, because it is a logical counterpart to the standard library function atoi.

:void itoa(int input, char *buffer, int radix)

itoa takes the integer input value input and converts it to a number in base radix. The resulting number (a sequence of base-radix digits) is written to the output buffer buffer.

Depending on the implementation, itoa may return a pointer to the first character in buffer, or may be designed so that passing a null buffer causes the function to return the length of the string that "would have" been written into a valid buffer.

For converting a number to a string in base 8 (octal), 10 (decimal), or 16 (hexadecimal), a Standard-compliant alternative is to use the standard library function sprintf.

K&R implementation

The function itoa appeared in the first edition of Kernighan and Ritchie's "The C Programming Language", on page 60. The second edition of "The C Programming Language" ("K&R2") contains the following implementation of itoa, on page 64. The book notes several issues with this implementation, including the fact that it does not correctly handle the most negative number −2wordsize-1. [For the solution to this exercise, see [http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_4 "K&R2 solutions"] on "clc-wiki.net".]

/* itoa: convert n to characters in s */ void itoa(int n, char s [] ) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n positive */ i = 0; do { /* generate digits in reverse order */ s [i++] = n % 10 + '0'; /* get next digit */ } while ((n /= 10) > 0); /* delete it */ if (sign < 0) s [i++] = '-'; s [i] = '


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Itoa (Си) — Функция itoa широко распространенное нестандартное расширение стандартного языка программирования Си. Ее использование не предусматривает переносимости, поскольку эта функция не определена ни в одном стандарте языка Си; тем не менее, зачастую… …   Википедия

  • itoa (Си) — У этого термина существуют и другие значения, см. Itoa (значения). Функция itoa  широко распространённое нестандартное расширение стандартного языка программирования Си. Ее использование не предусматривает переносимости, поскольку эта… …   Википедия

  • Itoa —   Itoa Clasificación cientí …   Wikipedia Español

  • Itoa (значения) — Itoa: Itoa научное название рода растений из семейства Ивовые (Salicaceae). itoa функция языка Си, не входящая в стандарт языка …   Википедия

  • Itoa — …   Википедия

  • ITOA — Information Technology Operations Analysis (Academic & Science » Universities) Information Technology Operations Analysis (Governmental » Military) Information Technology Operations Analysis (Business » General) …   Abbreviations dictionary

  • Итоа — Итоа …   Википедия

  • Cyclone (programming language) — Cyclone Appeared in 2006 (2006) Designed by AT T Labs Stable release 1.0 (May 8, 2006; 5 years ago (2006 05 08)) Influenced by …   Wikipedia

  • SNUSP — (un acronyme récursif signifiant SNUSP s Not Unix, but Structured PATH) est un langage de programmation exotique issu de la transformation du Brainfuck en un langage à deux dimensions, inspiré d un langage appelé PATH[réf. nécessaire]. Le… …   Wikipédia en Français

  • Stdlib.h — заголовок (заголовочный файл) стандартной библиотеки общего назначения языка Си, который содержит в себе функции, занимающиеся выделением памяти, контроль процесса выполнения программы, преобразования типов и другие. Заголовок вполне совместим с… …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”