I want to convert an int to a string without printing anything on my screen. For now I used sprintf, but this also printed the int to my screen. Also itoa is not supported by my compiler so I can't use that either.
Asked
Active
Viewed 528 times
1 Answers
-1
I assume You use ANSI C.
You cannot use itoa
because it's not a standard function.
sprintf
or snprintf
is dedicated to that.
Since You do not want to use sprintf
, make your own itoa
instead:
#include <stdio.h>
char* itoa(int i, char b[]){
char const digit[] = "0123456789";
char* p = b;
if(i<0){
*p++ = '-';
i *= -1;
}
int shifter = i;
do{ //Move to where representation ends
++p;
shifter = shifter/10;
}while(shifter);
*p = '\0';
do{ //Move back, inserting digits as u go
*--p = digit[i%10];
i = i/10;
}while(i);
return b;
}
original answer: here