itoa.cc 624 B

12345678910111213141516171819202122232425262728293031323334
  1. /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  2. Written by James Clark (jjc@jclark.com) */
  3. #include "lib.h"
  4. #define INT_DIGITS 19 /* enough for 64 bit integer */
  5. const char *
  6. our_itoa(int i)
  7. {
  8. /* Room for INT_DIGITS digits, - and '\0' */
  9. static char buf[INT_DIGITS + 2];
  10. char *p = buf + INT_DIGITS + 1; /* points to terminating '\0' */
  11. if (i >= 0)
  12. {
  13. do
  14. {
  15. *--p = '0' + (i % 10);
  16. i /= 10;
  17. } while (i != 0);
  18. return p;
  19. }
  20. else
  21. { /* i < 0 */
  22. do
  23. {
  24. *--p = '0' - (i % 10);
  25. i /= 10;
  26. } while (i != 0);
  27. *--p = '-';
  28. }
  29. return p;
  30. }