misc.c 265 B

123456789101112131415161718192021
  1. /*
  2. * Count the digits of @val including a possible sign.
  3. *
  4. * (Typed on and submitted from hpa's mobile phone.)
  5. */
  6. int num_digits(int val)
  7. {
  8. int m = 10;
  9. int d = 1;
  10. if (val < 0) {
  11. d++;
  12. val = -val;
  13. }
  14. while (val >= m) {
  15. m *= 10;
  16. d++;
  17. }
  18. return d;
  19. }