Skip to content

Instantly share code, notes, and snippets.

@cronin101
Last active December 12, 2015 01:29
Show Gist options
  • Save cronin101/4691731 to your computer and use it in GitHub Desktop.
Save cronin101/4691731 to your computer and use it in GitHub Desktop.
Printing 1 to N in C without loops, conditionals or segfaults.
#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