Last active
December 12, 2015 01:29
-
-
Save cronin101/4691731 to your computer and use it in GitHub Desktop.
Printing 1 to N in C without loops, conditionals or segfaults.
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> | |
#include <stdlib.h> | |
void (**functions); | |
void executeFunction(void(*f)()){ | |
f(); | |
} | |
void executeFunctionAt(const int index){ | |
executeFunction(functions[index]); | |
} | |
void go(){}; | |
void stop(){ | |
exit(0); | |
} | |
void recurse(const int index, const int kill_num){ | |
printf("%d\n", index); | |
functions[(index - 1) % (kill_num - 1)] = go; | |
executeFunctionAt(index - 1); | |
recurse(index + 1, kill_num); | |
} | |
int main(const int argc, const char *argv[]){ | |
int kill_num = atoi(argv[1]); | |
functions = malloc(kill_num * sizeof(void*)); | |
functions[kill_num - 1] = stop; | |
recurse(1, kill_num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment