Skip to content

Instantly share code, notes, and snippets.

@aprell
Created June 29, 2016 13:17
Show Gist options
  • Select an option

  • Save aprell/ee98ffc74c8d19bdb9debdd9ca760600 to your computer and use it in GitHub Desktop.

Select an option

Save aprell/ee98ffc74c8d19bdb9debdd9ca760600 to your computer and use it in GitHub Desktop.
Nested functions in GNU C
#include <stdio.h>
#include <assert.h>
#define defer_(x) \
({ typeof(x) fun(void) { return x; } fun; })
#define defer(n, x) \
typeof(x) (*n)() = defer_(x)
int main(void)
{
int a = 42;
defer (t1, printf("World\n"));
defer (t2, 1+2);
defer (t3, a++);
printf("Hello ");
t1();
assert(t2() == 3);
assert((t3(), a) == 43);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment