Skip to content

Instantly share code, notes, and snippets.

@ricogallo
Created October 28, 2016 12:27
Show Gist options
  • Save ricogallo/ba1aa9cb35467e8b7f9021d88f4deaac to your computer and use it in GitHub Desktop.
Save ricogallo/ba1aa9cb35467e8b7f9021d88f4deaac to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define coroutine_begin() static int state=0; switch(state) { case 0:
#define coroutine_return(x) { state=__LINE__; return x; case __LINE__:; }
#define coroutine_finish() }
int get_next(void) {
static int i = 0;
coroutine_begin();
while (1){
coroutine_return(++i);
coroutine_return(100);
}
coroutine_finish();
}
int main(void){
printf("i is %d\n", get_next()); /* Prints 'i is 1' */
printf("i is %d\n", get_next()); /* Prints 'i is 100' */
printf("i is %d\n", get_next()); /* Prints 'i is 2' */
printf("i is %d\n", get_next()); /* Prints 'i is 100' */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment