This is called a statement expression and creates a "lambda" (or nested function) and returns a pointer to it. It is GNU C-specific.
The macro expands to:
int (*max)(int, int) = ({ int _ (int x, int y) { return x > y ? x : y; } _; })
The _
at the end is like a return
.
The underscore is actually the name of the function that is created and "returned". It's used because it's an uncommonly-used identifier (for good reason; _
is quite possibly the least descriptive identifier possible).
The reason the statement expression is used is so _
won't be defined after the scope of the statement expression exits.
So, going through the macro:
#define lambda(ret_type, _body) ({ ret_type _ _body _; })
ret_type
is the return type of the "lambda". _
is the name of the function used inside it because it is an uncommon identifier name. _body
consists of the arguments and body of the function. The trailing _
"returns" the "lambda".
This code is found at Let's Destroy C (which is an appropriate name). You shouldn't use it. It will make your code work only on compilers that support GNU C extensions. Instead, just write a function or macro.
If you use constructs like this a lot or want more features, I suggest using C++. With C++ you can do something similar to this and have portable code.