问题描述
我有这块code的,我不知道它是如何工作
I have this piece of code and i don't know how it works
#include <stdio.h>
int main(void)
{
int numero = ({const int i = 10; i+10;});
printf("%d\n", numero); // Prints 20
return 0;
}
为什么如果我删除的第二部分(1 + 10),编译器会得到一个错误?
为什么括号必要吗?
Why if i delete the second part (i+10;), the compiler gets an error?Why are the brackets necessary?
感谢您^^!
推荐答案
这是一个。它执行其中的语句,并返回的最后一条语句计算的值。因此, NUMERO
初始化为 20
。如果您删除的第二部分,没有前pression作为最后的语句,所以它不能从语句前pression值。
It's a GCC statement expression. It executes the statements in it, and returns the value evaluated in the last statement. Thus numero
is initialized to 20
. If you delete the second part, there is no expression as the last statement, so it can't get a value from the statement expression.
大括号是必要的,从普通的C歧义它括号中的前pressions。
The braces are necessary to disambiguate it from ordinary C parenthesized expressions.
这篇关于用C怪异初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!