C: how to break apart a multi digit number into separate variables?
As a hint, getting the nth digit in the number is pretty easy; divide by 10 n times, then mod 10, or in C:
int nthdig(int n, int k){
while(n--)
k/=10;
return k%10;
}
int value = 123;
while (value > 0) {
int digit = value % 10;
// do something with digit
value /= 10;
}
The last digits of 123 is 123 % 10. You can drop the last digit of 123 by doing 123/10 -- using integer division this will give you 12. To answer your question about "how do I know how many digits you have" -- try doing it as described above and you will see how to know when to stop.
First, count the digits:
unsigned int count(unsigned int i) {
unsigned int ret=1;
while (i/=10) ret++;
return ret;
}
Then, you can store them in an array:
unsigned int num=123; //for example
unsigned int dig=count(num);
char arr[dig];
while (dig--) {
arr[dig]=num%10;
num/=10;
}