This behavior seems to be specfic to newer versions of Clang, and is a language extension called "blocks".

The Wikipedia article on C "blocks" also provides information which supports this claim:

Blocks are a non-standard extension added by Apple Inc. to Clang's implementations of the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Blocks are supported for programs developed for Mac OS X 10.6+ and iOS 4.0+, although third-party runtimes allow use on Mac OS X 10.5 and iOS 2.2+ and non-Apple systems.

Emphasis above is mine. On Clang's language extension page, under the "Block type" section, it gives a brief overview of what the Block type is:

Like function types, the Block type is a pair consisting of a result value type and a list of parameter types very similar to a function type. Blocks are intended to be used much like functions with the key distinction being that in addition to executable code they also contain various variable bindings to automatic (stack) or managed (heap) memory.

GCC also has something similar to blocks called lexically scoped nested functions. However, there are some key differences also note in the Wikipedia articles on C blocks:

Blocks bear a superficial resemblance to GCC's extension of C to support lexically scoped nested functions. However, GCC's nested functions, unlike blocks, must not be called after the containing scope has exited, as that would result in undefined behavior.

GCC-style nested functions also require dynamic creation of executable thunks when taking the address of the nested function. [...].

Emphasis above is mine.

Answer from Chris on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ lambda-expression-in-c
Lambda Expression in C++ - GeeksforGeeks
They improve code readability by keeping behavior close to where it is applied and remove the need for separate named functions. Used to assign custom logic to algorithms like sort, for_each, and find_if in C++ STL. Used in Callbacks and Event Handling for asynchronous tasks and events. ... #include <iostream> using namespace std; int main() { // Defining a lambda auto res = [](int x) { return x + x; }; // Using the lambda cout << res(5); return 0; }
Published ย  5 days ago
๐ŸŒ
Cprogramming.com
cprogramming.com โ€บ c++11 โ€บ c++11-lambda-closures.html
C++11 - Lambda Closures, the Definitive Guide - Cprogramming.com
How to begin Get the book ยท C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials
Discussions

simple lambda like functions in C
These nested functions arenโ€™t standard C and they wonโ€™t work on Clang or MSVC. More on reddit.com
๐ŸŒ r/C_Programming
26
11
March 12, 2024
Why does C not have lambdas/anonymous function expressions?
There are two basic ways to implement closures: A custom calling convention: Callers are aware they're calling a closure and so pass the closure context to the callee, perhaps as a hidden argument. Closures are nearly always implemented this way, including in C++. For C, as a lingua franca platforms would need to define this calling convention / ABI so that different implementers could all each others closures just as they can call each others functions. Since this doesn't exist, closures in language implementations using this approach are incompatible with C interop (ex. can't turn a C++ closure into a function pointer). Build a trampoline by allocating a little bit of executable memory, essentially like using a small JIT compiler. Callers do not need to be aware they're calling a closure, and a plain C calling convention is sufficient. GNU C closures are implemented this way, as are CPython's ctypes callbacks. However, frequently allocating executable memory requires significant trade-offs in performance or security. It may not even be possible on some platforms. This is also an implicit allocation โ€” which is not in the spirit of C โ€” and someone has to manage its lifetime. (GNU C manages it by using an automatic allocation.) Unless you're willing to make the trade-offs in the second approach โ€” which is available to you if you use GCC โ€” neither fits C well. More on reddit.com
๐ŸŒ r/C_Programming
35
6
August 7, 2022
I still don't understand the benefit of lambda functions
they're just...functions with different syntax That's exactly what they are. The main syntactical difference between the two is that a regular function definition is a statement, whereas a lambda function definition is an expression. Lambda functions can therefore be defined and then passed to another function in one step, without having to go through a local name (that's also why lambda functions are sometimes called anonymous functions, they don't necessarily have a particular name). Lambda is a convenience feature to more easily define small, one-off functions. More on reddit.com
๐ŸŒ r/learnpython
118
320
January 23, 2022
C++ now supported in AWS lambda

Uhhm, you could always use C++ on AWS Lambda.

It's nice to have a C++ runtime available, makes things easier, but nothing prevented you running native code on Lambda.

More on reddit.com
๐ŸŒ r/cpp
42
141
November 29, 2018
Top answer
1 of 2
10

This behavior seems to be specfic to newer versions of Clang, and is a language extension called "blocks".

The Wikipedia article on C "blocks" also provides information which supports this claim:

Blocks are a non-standard extension added by Apple Inc. to Clang's implementations of the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Blocks are supported for programs developed for Mac OS X 10.6+ and iOS 4.0+, although third-party runtimes allow use on Mac OS X 10.5 and iOS 2.2+ and non-Apple systems.

Emphasis above is mine. On Clang's language extension page, under the "Block type" section, it gives a brief overview of what the Block type is:

Like function types, the Block type is a pair consisting of a result value type and a list of parameter types very similar to a function type. Blocks are intended to be used much like functions with the key distinction being that in addition to executable code they also contain various variable bindings to automatic (stack) or managed (heap) memory.

GCC also has something similar to blocks called lexically scoped nested functions. However, there are some key differences also note in the Wikipedia articles on C blocks:

Blocks bear a superficial resemblance to GCC's extension of C to support lexically scoped nested functions. However, GCC's nested functions, unlike blocks, must not be called after the containing scope has exited, as that would result in undefined behavior.

GCC-style nested functions also require dynamic creation of executable thunks when taking the address of the nested function. [...].

Emphasis above is mine.

2 of 2
7

the C standard does not define lambdas at all but the implementations can add extensions.

Gcc also added an extension in order for the programming languages that support lambdas with static scope to be able to convert them easily toward C and compile closures directly.

Here is an example of extension of gcc that implements closures.

#include <stdio.h>

int(*mk_counter(int x))(void)
{
    int inside(void) {
        return ++x;
    }
    return inside;
}

int
main() {
    int (*counter)(void)=mk_counter(1);
    int x;
    x=counter();
    x=counter();
    x=counter();
    printf("%d\n", x);
    return 0;
}
๐ŸŒ
Hackaday
hackaday.com โ€บ 2019 โ€บ 09 โ€บ 11 โ€บ lambdas-for-c-sort-of
Lambdas For C โ€” Sort Of | Hackaday
November 2, 2023 - The map call ensures that the anonymous function is called once for each item.โ€ ยท Something, something, on a calculator. ... Please donโ€™t do this to C. If lambda was a useful feature K&R would have included it. There are reasons C is feature limited, and it becomes quite apparent when you look at the wretched code written by modern programmers in less performant languages. ... Fundamentalism is never good. I think this is a nice hack and example what kind of specifics gcc has.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ cpp โ€บ lambda-expressions-in-cpp
Lambda expressions in C++ | Microsoft Learn
Each instance of auto in a parameter list is equivalent to a distinct type parameter. auto y = [] (auto first, auto second) { return first + second; }; A lambda expression can take another lambda expression as its argument.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ simple lambda like functions in c
r/C_Programming on Reddit: simple lambda like functions in C
March 12, 2024 -

I was diving in reddit and I found the following post

I thought: maybe it is possible to do something similar in C with macros or something along those lines.

After some research, I found the following topic on GCC manual:
Statements and Declarations in Expressions

Well, with this construction, it's possible to instruct the compiler to define a function and call it in place, like we can do with lambdas. Look the example bellow:

#include <stdio.h>
int call_callback(void (*callback)()){
    callback();
}

void foo() {
    printf("foo\n");
}

int main(void) {
    call_callback(foo);
    call_callback(({
        void _() {
            printf("this is a lambda?\n");
        }
        (void (*)())_;
    }));
}

For me, it's very interesting. We encounter many situations and libraries that deal with callback functions, and personally, I often find myself declaring simple functions that are only called once. I believe that in these cases, this construct would work very well.

However, debugging it might be challenging.

๐ŸŒ
Medium
madhawapolkotuwa.medium.com โ€บ mastering-lambda-functions-in-c-a-complete-guide-with-practical-examples-e2cc3f10dfce
Mastering Lambda Functions in C++: A Complete Guide with Practical Examples | by Madhawa Polkotuwa | Medium
November 1, 2024 - The first Lambda captures all variables by value, freezing their values inside the Lambda, while the second captures all by reference, making it sensitive to changes. ... Lambda functions in C++ are a powerful way to make your code cleaner and more concise, especially for small, local operations.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-we-write-and-use-lambda-functions-in-pure-C-indirectly-instead-of-C
How do we write and use lambda functions in pure C indirectly instead of C++? - Quora
Answer (1 of 5): https://en.wikipedia.org/wiki/Anonymous_function#C_(non-standard_extension) > C (non-standard extension)[edit] The anonymous function is not supported by standard C programming language, but supported by some C dialects, such as GCC[52] and Clang. GCC[edit] GNU Compiler Colle...
๐ŸŒ
W3Schools
w3schools.com โ€บ cpp โ€บ cpp_functions_lambda.asp
C++ Lambda Functions
You can also pass a lambda function as an argument to another function. This is useful when you want to tell a function what to do, not just what data to use. In the example below, we send a small lambda function to another function, which then runs it twice:
๐ŸŒ
DZone
dzone.com โ€บ articles โ€บ all-about-lambda-functions-in-cfrom-c11-to-c17
All About Lambda Functions in C++ (From C++11 to C++17)
May 8, 2020 - Zero cost abstraction. Yes! You read it right. Lambda doesn't cost you performance, and it's as fast as a normal function. In addition, code becomes compact, structured, and expressive. ... In the above example, I have mentioned & in the capture list. This captures variable x and y as a reference.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ csharp โ€บ language-reference โ€บ operators โ€บ lambda-expressions
Lambda expressions - Lambda expressions and anonymous functions - C# reference | Microsoft Learn
January 24, 2026 - Convert a lambda expression that has one parameter and returns a value to a Func<T,TResult> delegate. In the following example, the lambda expression x => x * x, which specifies a parameter named x and returns the value of x squared, is assigned ...
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ why does c not have lambdas/anonymous function expressions?
r/C_Programming on Reddit: Why does C not have lambdas/anonymous function expressions?
August 7, 2022 -

It would not seem to hard to implement to allow a programmer to use a construct similar to:

int (*add)(int, int) = (int(int x, int y)){return x+y;};

This would simplify code that requires callback functions such as qsort or bsearch or various UI libraries that use callbacks to define, for example, a buttons behavior when pressed. Is there any specific reason they elected not to support this, and require us to define named static functions instead?

Top answer
1 of 7
8
There are two basic ways to implement closures: A custom calling convention: Callers are aware they're calling a closure and so pass the closure context to the callee, perhaps as a hidden argument. Closures are nearly always implemented this way, including in C++. For C, as a lingua franca platforms would need to define this calling convention / ABI so that different implementers could all each others closures just as they can call each others functions. Since this doesn't exist, closures in language implementations using this approach are incompatible with C interop (ex. can't turn a C++ closure into a function pointer). Build a trampoline by allocating a little bit of executable memory, essentially like using a small JIT compiler. Callers do not need to be aware they're calling a closure, and a plain C calling convention is sufficient. GNU C closures are implemented this way, as are CPython's ctypes callbacks. However, frequently allocating executable memory requires significant trade-offs in performance or security. It may not even be possible on some platforms. This is also an implicit allocation โ€” which is not in the spirit of C โ€” and someone has to manage its lifetime. (GNU C manages it by using an automatic allocation.) Unless you're willing to make the trade-offs in the second approach โ€” which is available to you if you use GCC โ€” neither fits C well.
2 of 7
6
Why are people obsessed with making C like python or whatever language they learned first? It's like asking why my bicycle only has two wheels, when your red wagon has four.
๐ŸŒ
SmartBear
smartbear.com โ€บ blog โ€บ c11-tutorial-lambda-expressions-the-nuts-and-bolts
C++11 Tutorial: Lambda Expressions โ€” The Nuts and Bolts of Functional Programming
November 1, 2012 - In our example, the lambda expression reports whether the current accountant object in the vector emps gets a salary that is both lower than the upper limit and higher than the minimum wage.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ significance-of-lambda-function-in-c-cplusplus
Significance of Lambda Function in C/C++
February 5, 2021 - Lambda Function โˆ’ Lambda functions are anonymous inline functions that don't require any implementation outside the scope where they are defined. They provide a concise way to write small functions directly at the point of use.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ lamda-unction-c-amit-nadiger
Lamda function in C++
June 18, 2023 - The lambda function multiply is converted into an object of the closure type ClosureType. The captured multiplier value is stored in the closure object, and the invocation of the lambda function is replaced with a call to the operator() function of the closure object ยท Example 2: When variables are captured by reference in a lambda function
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ language โ€บ lambda.html
Lambda expressions (since C++11) - cppreference.com
It is a public, constexpr,(since C++17) non-virtual, non-explicit, const noexcept member function of the closure object. The copy constructor and the move constructor are declared as defaulted and may be implicitly-defined according to the usual rules for copy constructors and move constructors. The destructor is implicitly-declared. If the lambda expression captures anything by copy (either implicitly with capture clause [=] or explicitly with a capture that does not include the character &, e.g.
๐ŸŒ
DEV Community
dev.to โ€บ sandordargo โ€บ lambda-expressions-in-c-4pj4
Lambda Expressions in C++ - DEV Community
September 4, 2020 - In the last parameter, we defined a lambda expression. It gets a character as a parameter and returns true or false depending on whether the passed in character is a digit or not. Luckily in the standard library, there is a function to do, meaning that we don't have to try to cast it, nor to check its ASCII value.
๐ŸŒ
Programiz
programiz.com โ€บ cpp-programming โ€บ lambda-expression
C++ Lambda
In the above example, we have created a lambda function that increments the value of a local variable num by 1.