itoa 函数

//my_itoa
char * my_itoa(int a)
{
    int n = a;
    if (a < 0)
    {
        a = -a;//如果是负数先转化为正数
    }
    static char str[100];//这里的str必须是静态变量或全局变量
    int i = 0;
    while (a>0)//从低位开始变为字符,最后进行翻转
    { 
        str[i++] = a % 10 + '0';//将数字转为字符串
        a = a / 10;
    }
    if (n < 0)//如果是负数加上‘-’号
    {
        str[i++] = '-';
    }
    str[i] = '\0';

    return reverse(str);
}


猜你喜欢

转载自blog.csdn.net/luoyir1997/article/details/80316398