In C:
#include
#include
char* itoa(int num)
{
/* log10(num) gives the number of digits; + 1 for the null terminator */
int size = log10(num) + 1;
char *x = malloc(size);
snprintf(x, size, "%d", num);
}
In C++:
#include
#include
#include
string itoa(int num)
{
stringstream converter;
converter << num;
return converter.str();
}