gpt4 book ai didi

c++ - C++中 "()"如何将语句转换为表达式?

转载 作者:IT老高 更新时间:2023-10-28 21:46:00 25 4
gpt4 key购买 nike

我有以下代码:

int main() {
int i=0;
int j=({int k=3;++i;})+1; // this line
return 0;
}

它编译并运行。如果我从“这一行”中删除 (),则它不会编译。

我只是好奇这里应用了什么语法规则。

{}包含2条语句,最后一条语句表示该代码块的“返回”值。那为什么它需要一个额外的 () 对来使这个返回值可用呢?

最佳答案

那是 statement expression ,它是一个 GCC 特定的扩展。


来自链接引用:

A compound statement enclosed in parentheses may appear as an expression in GNU C. This allows you to use loops, switches, and local variables within an expression.

复合语句是花括号括起来的语句 block 。

关于c++ - C++中 "()"如何将语句转换为表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52313218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com