在ANSI C中是否由括号表达式包围的复合语句(块)? [英] Are compound statements (blocks) surrounded by parens expressions in ANSI C?

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

问题描述

浏览Linux内核源码,我发现一些代码片段,括号中包围的语句块被视为表达式 a la lisp (或ML),即一个表达式,该值是最后一个值



例如:

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

我一直在看 ANSI C语法试图弄清楚这段代码如何适合解析树,但是没有成功。



所以,有人知道这个行为是由标准规定的还是只是GCC的特色?



更新:我已经尝试使用flag -pedantic,编译器现在给我一个警告:

 警告:ISO C禁止表达式内的支撑组


解决方案

它被称为表达式内的支撑组。



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中是否由括号表达式包围的复合语句(块)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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