Created
October 28, 2016 12:27
-
-
Save ricogallo/ba1aa9cb35467e8b7f9021d88f4deaac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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