复合语句(块)是否被 ANSI C 中的括号表达式包围? [英] Are compound statements (blocks) surrounded by parens expressions in ANSI C?
问题描述
浏览我发现的 Linux 内核源代码 某段代码,其中用括号括起来的语句块被视为表达式a la lisp(或ML),即一个表达式,其值是最后一个的值声明.
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.
例如:
int a = ({
int i;
int t = 1;
for (i = 2; i<5; i++) {
t*=i;
}
t;
});
我一直在看 ANSIC 语法试图弄清楚这段代码如何适合解析树,但我没有成功.
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.
那么,有没有人知道这种行为是标准规定的还是只是 GCC 的一个特性?
So, does anybody know if this behaviour is mandated by the standard or is just a peculiarity of GCC?
更新:我已经尝试使用标志 -pedantic 并且编译器现在给我一个警告:
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".
ANSI/ISO C 和 C++ 都不允许,但 gcc 支持.
It's not allowed by ANSI/ISO C nor C++ but gcc supports it.
这篇关于复合语句(块)是否被 ANSI C 中的括号表达式包围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!