<< J...J...JJava | Home | Your Java Is Being End-Of-Life'd >>

Friday C Quiz: Know Your Closures

You know what? This closure thing has gotten into my head. I know it's pointless and useless to think and talk about it all the time, but I can't help it.

Well, my loss is your gain. And today's Friday quiz will take you all the way back to C. Yes, old trusty C! Although ANSI C does not support closures, some C compilers provide nested function extensions that provide a limited subset of closure functionalities such as accessing variables in the outer function.

Q: Will the following C program compile and run under your C compiler? If so, what will it print?


  #include <stdio.h>
  
  int main() {
    int (*pFib)(int);
    int fib(int n) {
      return n == 0 ? 0 :
             n == 1 ? 1 :
             pFib(n - 1) + pFib(n - 2);
    }
    pFib = fib;
    printf("%d\n", pFib(6));
    return 0;
  }



Re: Friday C Quiz: Know Your Closures

I don't know why (perhaps because it is yet another Friday night looking at this blog and I'm losing it ;-), but the first thing that I thought of with this: can I paste this into a browser and see the output?

The reason is probably that I don't have a C compiler installed and don't really want to get one.

I wonder if there is a C 'compiler' in Javascript? It seems like there is an emulator for everything these days.

Re: Friday C Quiz: Know Your Closures

Take a look at this.

It's not in JavaScript, but it's a (non-REST) Web Service. And it's the first thing I thought of when I saw your comment.

Re: Friday C Quiz: Know Your Closures

re: web-site. That's pretty cool! I like it... though I'm worried about trojan horses in the EXEs ;-)

Re: Friday C Quiz: Know Your Closures

You can trust DJ.

C:\Downloads>dir program.exe
 Volume in drive C has no label.
 Volume Serial Number is 2807-627C

 Directory of C:\Downloads

04/05/2008  09:10 PM            46,476 program.exe
               1 File(s)         46,476 bytes
               0 Dir(s)  12,765,540,352 bytes free

C:\Downloads>program.exe
8

C:\DOWNLO~1>

Re: Friday C Quiz: Know Your Closures

There's also codepad.org, which compiles it, runs it, and sends the output to the web browser. Result: 8

Re: Friday C Quiz: Know Your Closures

re: trust DJ. It looks like it indeed. I meant in general though.... No disrespect intended!

re: codepad. WOW! Now we are talking... this is great! Thanks Ryan

Re: Friday C Quiz: Know Your Closures

Using the c compiler that comes with Mac OS X (i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465) ), the first compile generates:
error: nested functions are disabled, use -fnested-functions to re-enable

Good error message. I try it again with enabled nested-functions and run. The program prints:

8
Hmmm. So, I stick a small printf as the first line of the embedded "fib()" to see the value of n on entry.

Even more hmmmm...

Re: Friday C Quiz: Know Your Closures

It compiled fine on my PPC Panther edition of Mac OS X with gcc 3.3 (20030304).

re: hmmmm... It would be nice to print each n with an indentation proportional to the recursion level. If only I can remember how to parametrize the 3 in "%3d" in pritnf().


Add a comment Send a TrackBack