Skip to content

Instantly share code, notes, and snippets.

@derekchiang
Created November 17, 2014 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekchiang/71a2e1af1efee89999e8 to your computer and use it in GitHub Desktop.
Save derekchiang/71a2e1af1efee89999e8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
// A generator that generates 0..10 infinitely
int generator(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;
return i;
LABEL1:;
}
goto LABEL0;
}
int main() {
// Will print 1..10, then 1..10 again
for (int j = 0; j < 20; j++) {
printf("%d\n", generator());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment