fork download
  1. #include<stdio.h>
  2.  
  3. char buf = 0;
  4.  
  5. int getch();
  6. void ungetch(int c);
  7.  
  8. int main(void)
  9. {
  10. int c;
  11.  
  12. c = '*';
  13.  
  14. ungetch(c);
  15.  
  16. while((c=getch())!=EOF)
  17. putchar(c);
  18.  
  19. return 0;
  20. }
  21.  
  22. /* getch: get a (possibly) pushed back character */
  23.  
  24. int getch(void)
  25. {
  26. int c;
  27.  
  28. if(buf!=0)
  29. c = buf;
  30. else
  31. c = getchar();
  32.  
  33. buf =0;
  34. return c;
  35. }
  36.  
  37. /* ungetch: push a character back into input */
  38.  
  39. void ungetch(int c)
  40. {
  41. if(buf != 0)
  42. printf("ungetch: too many characters\n");
  43. else
  44. buf = c;
  45. }
  46.  
Success #stdin #stdout 0s 9432KB
stdin
jk
stdout
*jk