C: how to break apart a multi digit number into separate variables?

mugetsu picture mugetsu · Feb 15, 2012 · Viewed 131.8k times · Source

Say I have a multi-digit integer in C. I want to break it up into single-digit integers.

123 would turn into 1, 2, and 3.

How can I do this, especially if I don't know how many digits the integer has?

Answer

Tony picture Tony · Feb 15, 2012
int value = 123;
while (value > 0) {
 int digit = value % 10;
 // do something with digit
 value /= 10;
}