C地块成为前pression:({int类型的= 1; INT B = 2; A + B;})等于3 [英] C block becomes expression: ( {int a = 1; int b = 2; a+b;} ) equals 3

查看:106
本文介绍了C地块成为前pression:({int类型的= 1; INT B = 2; A + B;})等于3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 http://en.wikipedia.org/wiki/C_$ p $#pprocessor ​​的Multiple_evaluation_of_side_effects,我碰到这个例子就是:

While reading http://en.wikipedia.org/wiki/C_preprocessor#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?

您可以使用完全一样的功能,即 MAX(1,2)是一个前pression评价为2。

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

我的问题是,如何({说明书列表最后-EX pression;})结构评估到最后-EX $的价值p $ pssion?的具体,这是什么构造的解析树是什么样子?我以为 {} 总是意味着一个复合语句和语句没有值。我试着在 C 语法周围挖,仍然无法弄清楚这个问题了。

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.

推荐答案

这是所谓的声明前pressions 的。这不是标准的C

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

这篇关于C地块成为前pression:({int类型的= 1; INT B = 2; A + B;})等于3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆