《c程序设计语言》读书笔记-3.4-数字转字符串

#include <io.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

void reverse(char s[])
{
    int c,i,j;

    for(i = 0,j = strlen(s) - 1;i < j;i++,j--)
        c = s[i],s[i] = s[j],s[j] = c;
}

void itoa(int n,char s[])
{
    int i,sign;

    sign = n,i = 0;

    do
    {
        s[i++] = abs(n % 10) + '0';
    }while((n /= 10) != 0);

    if(sign < 0)
        s[i++] = '-';
        s[i] = '\0';
        reverse(s);
}

int main()
{
    int n,i;
    char s[50];
    n = -2147483648;

    itoa(n,s);

    for(i = 0;i < strlen(s);i++)
        printf("%c  ",s[i]);


	return 0;
}


posted @ 2014-11-11 11:03  司空格子Ored  阅读(187)  评论(0编辑  收藏  举报