c - 复合语句( block )是否被 ANSI C 中的括号表达式包围?

标签 c gcc standards expression

浏览我找到的 Linux 内核源代码 some piece of code其中被括号包围的语句 block 被视为表达式 a la lisp(或 ML),也就是说,表达式的值是最后一条语句的值。

例如:

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

我一直在查看 ANSI C grammar试图找出这段代码如何适合解析树,但我没有成功。

那么,有人知道这种行为是标准强制要求的还是只是 GCC 的一个特性?

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

warning: ISO C forbids braced-groups within expressions

最佳答案

这不是标准的 C。它是一个名为 statement expressions 的 gcc 扩展。 .您可以找到 C 扩展的完整列表 here .这实际上是 many gcc extensions used in the Linux kernel 之一看起来像clang supports this too尽管它没有在文档中明确命名。

如您所见,最后一个表达式用作表达式的值,文档说(强调我的):

The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct. (If you use some other kind of statement last within the braces, the construct has type void, and thus effectively no value.)

其中一个主要好处是制作安全 宏,避免对具有副作用的参数进行多次求值。给出的示例使用了这个不安全的宏:

#define max(a,b) ((a) > (b) ? (a) : (b))

它对 ab 求值两次,可以使用语句表达式重写以消除此问题,如下所示:

#define maxint(a,b) \
   ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) 

请注意,需要显式使用 int 可以使用另一个 gcc 扩展来修复 Typeof :

#define max(a,b) \
   ({ typeof (a) _a = (a), _b = (b); _a > _b ? _a : _b; }) 

请注意 clang also supports typeof .

关于c - 复合语句( block )是否被 ANSI C 中的括号表达式包围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22809979/

相关文章:

json - JSON 中的顶级值是否必须是数组或对象?

c++ - c++标准中有多少个头文件?

c - 为什么这些可以声明为整数?

c - 这个递归函数如何返回正确答案?

c - 这里的算法发生了什么,代码与 C 中的递归问题有关?

使用arm-none-eabi-gcc和bool变量的编译器错误

linux - 在 Qemu 上运行的 RISC-V Linux

c++ - 可以将自旋锁与 O(1) 非内存连续代码一起使用吗?

pdf - 日期字符串的正确格式是什么?

c - Antlr 4解析大型c文件需要永远