Alex Rivera | Logout

Printing a number without using *printf

Asked 2011-04-03T08:06:35.543
12

Is it possible to print (to stdout, or a file) an number (int, float, double, long, etc.) without actually using any of the *printf functions (printf, fprintf, sprintf, snprintf, vsprintf, …)?

c
Edit
Report

1 Answer

10

If your libc contains an itoa() function, you can use it to convert an integer to a string.
Otherwise you'll have to write the code to convert a number to a string yourself.

itoa() implementation from C Programming Language, 2nd Edition - Kernighan and Ritchie page 64:

/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
   int i, sign;

   if ((sign = n) < 0)  /* record sign */
      n = -n;           /* make n positive */
   i = 0;
   do {  /* generate digits in reverse order */
      s[i++] = n % 10 + '0';  /* get next digit */
   } while ((n /= 10) > 0);   /* delete it */
   if (sign < 0)
      s[i++] = '-';
   s[i] = '\0';
   reverse(s);
}
answered 2011-04-03T08:09:55.197

Your Answer