c语言版本的coroutine

#include <stdio.h>

int function(void) {
    static int i, state = 0;
    switch (state) {
        case 0: goto LABEL0;
        case 1: goto LABEL1;
    }
    LABEL0: /* start of function */
    for (i = 0; i < 10; i++) {
        state = 1; /* so we will come back to LABEL1 */
        return i;
        LABEL1:; /* resume control straight after the return */
    }
}

int main(){
  printf(":%d\n" , function());
  printf(":%d\n" , function());
  printf(":%d\n" , function());
  printf(":%d\n" , function());
  printf(":%d\n" , function());
  return 0;
}

想过,但是没想到,还真有啊!

posted @ 2019-01-16 21:00  友哥  阅读(193)  评论(0编辑  收藏  举报