From fdd66ab773f88dcf31fc55b647d6d2de7f0647cf Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Wed, 15 Jun 2022 12:09:19 +0530 Subject: add 4.12.c --- 4.12.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 4.12.c diff --git a/4.12.c b/4.12.c new file mode 100644 index 0000000..3e3e037 --- /dev/null +++ b/4.12.c @@ -0,0 +1,38 @@ +#include + +#define MAXLEN 30 + +char* itoa(char str[], int num); + +int +main(void) +{ + char str[MAXLEN]; + int num; + + num = -69420; + + printf("%d\n", num); + printf("%s\n", itoa(str, num)); + + return 0; +} + +char* +itoa(char str[], int num) +{ + static int i = 0; + + if (num < 0) { + num = -num; + str[i++] = '-'; + } + + if (num/10) + itoa(str, num/10); + + str[i++] = '0' + (num % 10); + str[i] = '\0'; + + return str; +} -- cgit v1.2.3