// // main.c // ctest // // Created by liweijian on 13-3-18. // Copyright (c) 2013年 liweijian. All rights reserved. // #include <stdio.h> #include <Block.h> //闭包 typedef void(*func_t)(); func_t test() { void func1() { printf("%s\n", __func__); printf("in func\n"); }; return func1; } //block1 #define test2() ({ \ char _a = 'a'; \ _a++; \ _a; \ }) //block2 typedef int (^IntBlock)(); IntBlock MakeCounter(int start, int increment) { __block int i = start; return Block_copy( ^ { int ret = i; i += increment; return ret; }); } int main(int argc, const char * argv[]) { test()(); int i = test2(); printf("%d\n", i); IntBlock mycounter = MakeCounter(5, 2); printf("First call: %d\n", mycounter()); printf("Second call: %d\n", mycounter()); printf("Third call: %d\n", mycounter()); /* because it was copied, it must also be released */ Block_release(mycounter); return 0; }