Thursday, December 3, 2009

Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal

void putlong(unsigned long x)
{
// we know that 32 bits can have 10 digits. 2^32 = 4294967296
for (unsigned long y = 1000000000; y > 0; y /= 10) {
putchar( (x / y) + '0');
x = x % y;
}
}

0 comments:

Post a Comment