Skip to content

Instantly share code, notes, and snippets.

@opsJson
Last active December 25, 2021 23:30
Show Gist options
  • Select an option

  • Save opsJson/0617d98b6897f6d3c2563994d28061b1 to your computer and use it in GitHub Desktop.

Select an option

Save opsJson/0617d98b6897f6d3c2563994d28061b1 to your computer and use it in GitHub Desktop.
Javascript anonymous functions in C.
#include <stdio.h>
#include <unistd.h>
//create a function that recieves a function pointer, and any arguments
void _foo(void (*function)()) {
function("Foo", 555);
}
void _setTimeout(void (*function)(), float time) {
sleep((float)time/1000);
function();
}
/* define a macro that creates a function with the given parameters and func, then, pass them
to its predefined function */
#define foo(x, y) {void func x {y} _foo(func);}
#define setTimeout(x, y, z) {void func x {y} _setTimeout(func, z);}
int main() {
foo((char *s, int i), {
printf("%s: %i\n", s, i);
});
setTimeout((), {
printf("Hello, world!\n");
}, 3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment