Skip to content

Instantly share code, notes, and snippets.

@bynect
Last active June 7, 2021 10:04
Show Gist options
  • Save bynect/947a048e8dee104087c2efe8e013e2de to your computer and use it in GitHub Desktop.
Save bynect/947a048e8dee104087c2efe8e013e2de to your computer and use it in GitHub Desktop.
Anonymous functions in C using GCC extensions.
#ifndef _LAMBDA_H
#define _LAMBDA_H
#define LAMBDA(stmt, ret, ... /*args*/) \
({ ret __fun__(__VA_ARGS__) { stmt; }; __fun__; })
#endif
#include <stdio.h>
#include "lambda.h"
int main()
{
void (*fun) (int, int) = LAMBDA(
printf("%d + %d == %d", a1, a2, a1 + a2),
void,
int a1, int a2
);
fun(123, 456);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment