Created
November 17, 2014 02:11
-
-
Save derekchiang/71a2e1af1efee89999e8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
// 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