11

And, if it does, how do you use one? (syntax)

Also, why does or why doesn't C support lambda expressions?

shosh
  • 113
  • 1
  • 4

4 Answers4

10

No, C has no support for lambda expressions.

If you're willing to use C++, Boost has a library that emulates lambdas. Also, C++0x will have built-in support for lambda expressions.

There wasn't a huge demand for lambda expression support in C at the time, so the language didn't support it.

In silico
  • 51,091
  • 10
  • 150
  • 143
5

C does not support lambda expressions, nor any other ways (within the language's standard) to dynamically create functions -- all functions, per the standard, are created at compile time. I guess the reason is to keep the language small, simple, lean, and very fast, with hardly any "runtime library" support necessary -- crucial for a language that's so widely used in programming operating systems, device drivers, embedded applications, and so forth.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 5
    Lambda expression does not require dynamically creating a function. It's just creating an object that refers to a (possibly nameless) function that's already compiled. – Mike Dunlavey May 02 '10 at 00:32
  • @Mike, in C you can refer to existing functions via a pointer (indeed, a _mention_ of the function's name "decays" to a pointer to it, so that's quite elegant and doable). – Alex Martelli May 02 '10 at 00:57
  • 1
    I think Mike has a point. You could in theory have in C a lambda-style syntax with no closures, to define within an expression a function like `x => x*x` and evaluate to a pointer to that function, all without breaking the constraint you mention. It's only when you want your lambda to access its surrounding scope that it becomes impossible with a typical C function pointer consisting just of the address of the code. Whether such context-free functions should rightly be called "lambdas" or not, I don't know. – Steve Jessop May 02 '10 at 01:10
  • And now I'm wondering what (if anything) could be done in a C implementation where function pointers are bigger than object pointers, so that they can contain a code address as normal but also a context pointer (which would be null for named C functions, and point to a chunk of stack for anonymous functions defined using some extension syntax). Obviously that's way more hassle than the C standard wants to inflict on implementers. But possibly less hassle than C++ pointers-to-members-of-virtual-base-classes. – Steve Jessop May 02 '10 at 01:12
  • @Steve, Clang has "lambda" support in C using *blocks*. See the spec at http://clang.llvm.org/docs/BlockLanguageSpec.txt and http://stackoverflow.com/questions/2395040/how-do-clang-blocks-work – Johannes Schaub - litb May 02 '10 at 10:25
3

No, C doesn't have lambda expressions (or any other way to create closures).

This is likely so because C is a low-level language that avoids features that might have bad performance and/or make the language or run-time system more complex.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
0

I saw this today: https://github.com/wd5gnr/clambda/blob/master/clambda2.c

See here the code of the link above:

#include <stdio.h>


float thelist[]={ 3.141, 6.02, 42.0, 0.7 };

#define lambda(lambda$_ret, lambda$_args, lambda$_body)\
  ({\
    lambda$_ret lambda$__anon$ lambda$_args\
      lambda$_body\
    &lambda$__anon$;\
  })


float average_apply(float (*fn)(float inp))
{
    int i,n=sizeof(thelist)/sizeof(thelist[0]);
    float avg=0.0;
    for (i=0;i<n;i++)
    {
        avg+=fn(thelist[i]);
        printf("Running sum=%f\n", avg);
    }

    return avg/n;
}

int main(int argc, char *argv[])
{
    printf("%f\n",average_apply(lambda(float,(float x),{ return 2*x; })));
    printf("%f\n",average_apply(lambda(float,(float x),{ return x/3.0; })));

    return 0;
}

And works fine in gcc.

PerduGames
  • 1,108
  • 8
  • 19