fork(22) download
  1. #include <stdio.h>
  2.  
  3. int naive_show_int(int x) {
  4. char buf[32];
  5. char *p = buf + sizeof(buf);
  6. *--p = 0;
  7. int negative = 0;
  8. if (x < 0) {
  9. x = -x;
  10. negative = 1;
  11. }
  12. while (x > 0) {
  13. if (x <= 0)
  14. return -1;
  15. int digit = '0' + x % 10;
  16. if (digit < '0' || digit > '9')
  17. return -1;
  18. *--p = digit;
  19. x /= 10;
  20. }
  21. if (negative)
  22. *--p = '-';
  23. puts(p);
  24. return 0;
  25. }
  26.  
  27. int main() {
  28. int r;
  29. r = naive_show_int(123456789);
  30. printf("r = %d\n", r);
  31. r = naive_show_int(-123456789);
  32. printf("r = %d\n", r);
  33. r = naive_show_int(0x80000000);
  34. printf("r = %d\n", r);
  35. return 0;
  36. }
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
123456789
r = 0
-123456789
r = 0
-./,),(-*,(
r = 0