Skip to content

Instantly share code, notes, and snippets.

@jiro4989
Last active August 23, 2020 14:12
Show Gist options
  • Save jiro4989/5d0305b062bbe0f2105d08472d10504a to your computer and use it in GitHub Desktop.
Save jiro4989/5d0305b062bbe0f2105d08472d10504a to your computer and use it in GitHub Desktop.
FizzBuzz with goto
#include <stdio.h>
#include <string.h>
/*
```bash
gcc goto.c
./a.out
```
*/
int main(void) {
const int MAX = 100;
int i = 1;
char text[256];
loop:
goto append_fizz; append_fizz_end:
goto append_buzz; append_buzz_end:
goto set_number; set_number_end:
goto print; print_end:
goto clear_text; clear_text_end:
goto goto_end; goto_end_end:
goto increment_counter; increment_counter_end:
goto loop;
append_fizz:
if (i % 3 == 0) strcat(text, "Fizz");
goto append_fizz_end;
append_buzz:
if (i % 5 == 0) strcat(text, "Buzz");
goto append_buzz_end;
set_number:
if (text[0] == '\0') sprintf(text, "%d", i);
goto set_number_end;
print:
printf("%s\n", text);
goto print_end;
clear_text:
text[0] = '\0';
goto clear_text_end;
goto_end:
if (MAX < i) goto end;
goto goto_end_end;
increment_counter:
i++;
goto increment_counter_end;
end:
return 0;
}
#include <stdio.h>
#define MAX_SIZE 256
int main(void) {
const int MAX = 100;
int i = 1;
char text1[MAX_SIZE];
char text2[MAX_SIZE];
int append_type;
int unused_var;
loop:
goto append_fizz; append_fizz_end:
goto append_buzz; append_buzz_end:
goto set_number; set_number_end:
goto print; print_end:
goto clear_text; clear_text_end:
goto goto_end; goto_end_end:
goto increment_counter; increment_counter_end:
goto loop;
append_fizz:
if (0 == i % 3) {
text2[0] = 'F';
text2[1] = 'i';
text2[2] = 'z';
text2[3] = 'z';
text2[4] = '\0';
append_type = 1;
goto strcat; strcat_append_fizz_end:
unused_var = 1;
}
goto append_fizz_end;
append_buzz:
if (0 == i % 5) {
text2[0] = 'B';
text2[1] = 'u';
text2[2] = 'z';
text2[3] = 'z';
text2[4] = '\0';
append_type = 2;
goto strcat; strcat_append_buzz_end:
unused_var = 1;
}
goto append_buzz_end;
set_number:
if ('\0' == text1[0]) sprintf(text1, "%d", i);
goto set_number_end;
print:
printf("%s\n", text1);
goto print_end;
clear_text:
text1[0] = '\0';
goto clear_text_end;
goto_end:
if (MAX < i) goto end;
goto goto_end_end;
increment_counter:
i++;
goto increment_counter_end;
strcat:
for (int j=0; j<MAX_SIZE; j++) {
if ('\0' == text1[j]) {
for (int k=0; k<MAX_SIZE; k++) {
text1[j+k] = text2[k];
}
goto strcat_loop_break;
}
}
strcat_loop_break:
if (1 == append_type) goto strcat_append_fizz_end;
if (2 == append_type) goto strcat_append_buzz_end;
end:
return 0;
}
#include <stdio.h>
#include <string.h>
int main(void) {
const int MAX = 100;
int i = 1;
char text[256];
while (1) {
if (i % 3 == 0) strcat(text, "Fizz");
if (i % 5 == 0) strcat(text, "Buzz");
if (text[0] == '\0') sprintf(text, "%d", i);
printf("%s\n", text);
text[0] = '\0';
if (MAX < i) {
break;
}
i++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment