开发者

C block becomes expression: ( {int a = 1; int b = 2; a+b;} ) equals 3

While reading http://en.wikipedia.org/wiki/C_pre开发者_JS百科processor#Multiple_evaluation_of_side_effects, I came across this example:

\#define max(a,b) \
   ({ typeof (a) _a = (a); \
       typeof (b) _b = (b); \
     _a > _b ? _a : _b; }) // WHY DOES THIS LINE WORK?

Which you can use exactly like a function, i.e. max(1,2) is an expression evaluating to 2.

My QUESTION is, How does ({ statment-list last-expression; }) construct evaluate to the value of last-expression? Specifically, what does a parse tree of this construct look like? I thought { } always meant a compound-statement, and statements have no values. I tried digging around in the C grammar and still couldn't figure this problem out.


This is a GCC extension called Statement Expressions. It's not standard C.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜