Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created November 23, 2009 10:02
Show Gist options
  • Select an option

  • Save shaobin0604/240992 to your computer and use it in GitHub Desktop.

Select an option

Save shaobin0604/240992 to your computer and use it in GitHub Desktop.
tcpl-ex-4-12.c
/*
* Exercise 4-12. Adapt the ideas of printd to write a recursive version of
* itoa; that is, convert an integer into a string by calling a recursive
* routine.
*/
#include <math.h>
#include <string.h>
void itoa(int n, char s[])
{
static int i;
if (n / 10)
itoa(n / 10, s);
else
{
i = 0;
if (n < 0)
s[i++] = '-';
}
s[i++] = abs(n) % 10 + '0';
s[i] = '\0';
}
int main(void)
{
int n = 100;
char s[10];
char exp[] = "100";
itoa(n, s);
if (strcmp(s, exp) == 0)
printf("assert success.\n");
else
printf("assert fail.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment