itoa函数的实现

简介:

void itoa(int n, char *s)

{

  int sign;

  char *t = s;

  

  if ((sign = n) < 0)

    n = -n;

  

  do

  {

    *s++ = n % 10 + '0';

  } 

  while ((n /= 10) >0);

  

  if (sign < 0)

    *s++ = '-';

  

  *s = '\0';

  

  reverse(t);

}


void reverse(char *s)

{

  int c;

  char *t;

  

  for (t = s + (strlen(s) - 1); s < t; s++, t-- )

  {

    c = *s;

    *s = *t;

    *t = c;

  }

}

原文来自:http://blog.21ic.com/user1/5433/archives/2008/54208.html



本文转自 Linux_woniu 51CTO博客,原文链接:http://blog.51cto.com/linuxcgi/1965340


相关文章
|
3月前
|
数据格式
sprintf函数
sprintf函数
18 0
|
8月前
模拟实现atoi函数
模拟实现atoi函数
|
9月前
strstr(str1,str2) 函数与sscanf()函数功能详解
strstr(str1,str2) 函数与sscanf()函数功能详解
|
9月前
itoa随手记(itoa是什么,itoa怎么用)
itoa随手记(itoa是什么,itoa怎么用)
111 0
strcmp函数详解
如果字符串不一样,并且字符串1>字符串2,则返回值>0.相反返回值小于零。
248 0
|
存储
atoi函数的初步实现到完美优化
atoi函数的初步实现到完美优化
152 0
atoi函数的初步实现到完美优化
<<C>>模拟atoi函数
<<C>>模拟atoi函数
61 0
atoi函数
atoi函数
119 0
itoa()函数与atoi()函数
1、itoa()函数(整型转字符) 以下是用itoa()函数将整数转换为字符串的一个例子: # include # include void main (void) { int num = 100; char str[...
1059 0