是复合语句(块)由ANSI C括号前pressions包围? [英] Are compound statements (blocks) surrounded by parens expressions in ANSI C?

查看:313
本文介绍了是复合语句(块)由ANSI C括号前pressions包围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览Linux内核源,我发现一些片$的C $ç在这里用括号包围的语句块被视为前pression的一拉口齿不清的(或ML),也就是说,一个前pression其价值是最后一条语句的值。

例如:

  int类型的=({
    INT I;
    INT T = 1;
    为(ⅰ= 2; I&小于5;我++){
        T * = I;
    }
    吨;
});

我一直在看 ANSI C语法的试图找出如何这块code会适合在解析树,但我没有成功。

所以,没有任何人知道,如果这种行为是由标准规定的,或仅仅是一个GCC的特点?

更新:我试过旗-pedantic和编译器现在给我一个警告:

 警告:ISO C禁止前pressions内支撑基


解决方案

这就是所谓的EX pression内支撑组。

这不是由ANSI / ISO C允许,也不C ++,但GCC支持它。

Browsing the Linux kernel sources I found some piece of code where a block of statements surrounded by parenthesis is treated as a expression a la lisp (or ML), that is, an expression which value is the value of the last statement.

For example:

int a = ({
    int i;
    int t = 1;
    for (i = 2; i<5; i++) {
        t*=i;
    }
    t;
});

I've been looking at the ANSI C grammar trying to figure out how this piece of code would fit in the parse tree, but I haven't been successful.

So, does anybody know if this behaviour is mandated by the standard or is just a peculiarity of GCC?

Update: I've tried with the flag -pedantic and the compiler now gives me a warning:

warning: ISO C forbids braced-groups within expressions

解决方案

It's called "braced-group within expression".

It's not allowed by ANSI/ISO C nor C++ but gcc supports it.

这篇关于是复合语句(块)由ANSI C括号前pressions包围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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