I have an integer in C language and I want to create a char array and insert the last digit each time using modulu 10. After that I need to print the array from end to beginning in order to get the number printed in the right order. I know it sounds weird, but I need to do it exactly like this.
But something is not working. The array is created fine, but than the char printing isn't working. This is what I did:
int baseChange (int base, int newBase, int num)
{
int baseTen = toBaseTen(base, num); //give me a number on base 10
char finalBase[24];
int i;
for (i = 0; baseTen != 0; i++)
{
int remain = baseTen % newBase;
finalBase[i] = (char)(remain); //insert the last digit to the array
baseTen /= newBase;
}
// Print finalBase from end to beginning in order to get the final number in the correct order:
for (i--; i >= 0; i--)
{
printf("i is: %d... %c", i, finalBase[i]);
}
return SUCCESS_EXIT;
}
If I print finalBase[i] during the first for loop, I get the right numbers, but after that, the second for loop is not printing anything. Why?


finalBase[i] + '0'iafter the first loop? What isbaseTenat the start of the first loop? What doestoBaseTendo? This whole algorithm seems fundamentally flawed: An integer is just an integer - there are no bases, or characters, just raw bits. When you represent an integer as a string, you do so in a given base. The fact that you have a function, which takes an integer, and returns an integer, but claims to be changing its base makes absolutely zero sense.%cto%d.