fork(1) download
  1. #include <stdio.h>
  2.  
  3. #define DECLARE() int state = 0
  4.  
  5. #define BEGIN switch (state) { \
  6.   case 0:
  7.  
  8. #define YIELD(val) do { \
  9.   state = __LINE__; \
  10.   return val; \
  11.   case __LINE__: \
  12.   ; \
  13.   } while (0)
  14.  
  15. #define END }
  16.  
  17. char* next()
  18. {
  19. static DECLARE();
  20. BEGIN;
  21. YIELD("мама");
  22. YIELD("мыла");
  23. YIELD("лалку");
  24. END;
  25. return 0;
  26. }
  27. char* kakoi()
  28. {
  29. static DECLARE();
  30. static int i;
  31. BEGIN;
  32. YIELD("Какой ");
  33. YIELD("yield ");
  34. for (i=0;i<3;++i)
  35. YIELD(")");
  36. END;
  37. return 0;
  38. }
  39. int main()
  40. {
  41. char* str;
  42. while (str = next()) {
  43. printf("%s ", str);
  44. }
  45. putchar('\n');
  46. while (str = kakoi()) {
  47. printf("%s", str);
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5512KB
stdin
Standard input is empty
stdout
мама мыла лалку 
Какой yield )))