I am writing a bit of an operating system, and I require the ability to print the addition of a variable. I have a working atoi function and so I reversed it to give me an itoa function. There is no way to access free memory, so I need to figure out the the number of digits in order to create the proper sized char array. I figured I would need to divide by 10 to get that, and then modulus the values into the correct spot. This is my c code so far:
char* itoa(int res) {
int size = 0;
int t = res;
while(t / 10 != 0) {
t = t/10;
size ++;
}
char ret[size+1];
ret[size] = '\0';
t = res;
int i = size - 1;
while(i > 0) {
ret[i] = t % 10;
t = t/10;
i--;
}
return ret;
}
As of right now, it prints nothing. What is wrong with it?