/* * uint64_to_string.c * * Converts a unsigned 64-bit integer to a printable string. * * $ gcc -g -Wall -pedantic -o foo foo.c * $ ./foo * 1234 is 1234 * 4321 is 4321 * 0 is 0 * 2147483646 is 2147483646 * */ #include #include #include #include // <-- this is the only header uint64_to_string needs #include // <-- this is needed for strlen(3) char *uint64_to_string(uint64_t n); int main(int argc, char *argv[]) { int ints[] = { 1234, 4321, 0, INT_MAX-1, 1234576890 }; char *s; int i; for(i=0; i<5; i++) { s = uint64_to_string(ints[i]); printf("%d is ", ints[i]); fflush(stdout); write(1, s, strlen(s)); write(1, "\n", 1); } } char * uint64_to_string(uint64_t n) { static char s[32] = "0000000000000000000000000000000\0"; int i; for(i=30; i>0; i--) { s[i] = (n % 10) + '0'; n = n / 10; if(n == 0) break; } return &s[i]; }