How does this C code have lambda

This code:

#include <stdio.h>

int main()
{
    void (^a)(void) = ^ void () { printf("test"); } ;
    a();
}

Compile without warning with clang -Weverything -pedantic -std = c89 (version clang-800.0.42.1) and print test.

I could not find any information about the C standard having lambda, and also gcc has its own syntax for lambda, and it would be strange if they did this if there was a standard solution.

+4
source share
2 answers

This behavior is similar to the new versions of Clang and is an extension of the language called "blocks . "

The C block Wikipedia article also contains information that supports this statement:

- , Apple Inc. Clang C, ++ Objective-C, , lambda, . , Mac OS X 10.6+ iOS 4.0+, Mac OS X 10.5 iOS 2.2+ Apple.

. Clang " " , :

, , . , , () () .

GCC , . , C:

GCC- C .. GCC, , , , undefined.

GCC . [...].

.

+2

C lambdas , .

Gcc , , C .

gcc, .

#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;
}
+2
source

Source: https://habr.com/ru/post/1667023/


All Articles