Forward or backward?
Assuming a positive integer:
unsigned int n = 1191223;
while (n != 0) {
doSomething (n % 10);
n /= 10;
}
... will work from the smallest to the largest, or ...
EDIT I forgot everything about this non-working solution that I had here. Please note that Very Smart People ™ seems to consistently use the smallest and largest iteration (both the Linux kernel and GLibC printf, for example, just iterate backwards), but here you can drop back if you really don't want to use snprintfany then the reason ...
int left_to_right (unsigned int n) {
unsigned int digit = 0;
if (0 == n) {
doSomething (0);
} else {
digit = pow(10, 1.0+ floor(log10(n)));
while (digit /= 10) {
doSomething ( (n / digit) % 10 );
}
}
}
, , log10 pow, snprintf,
int left_to_right_fixed_max (unsigned int n) {
unsigned int digit = 1000000000;
unsigned int n10 = 10 * n;
if (0 == n) {
doSomething (0);
} else {
while (digit > n10) { digit /= 10; }
while (digit /= 10) {
doSomething ( (n / digit) % 10 );
}
}
}
... , /, .
int left_to_right (unsigned int n) {
static const unsigned int digit [] =
{ 1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000
};
static const unsigned char max_place = 10;
unsigned char decimal;
unsigned char place;
unsigned char significant = 0;
if (0 == n) {
doSomething (0);
} else {
place = max_place;
while (place--) {
decimal = 0;
while (n >= digit[place]) {
decimal++;
n -= digit[place];
}
if (decimal | significant) {
doSomething (decimal);
significant |= decimal;
}
}
}
}
... http://www.piclist.com/techref/language/ccpp/convertbase.htm .