C 中的匿名代码块
这样的声明意味着什么?
int x = ( { int a; scanf( "%d", &a ); a ; } ) ;
它的编译和运行相当于:
int x;
scanf( "%d", &x );
它似乎像某种匿名函数调用之类的东西,但我不确定。我以前没有遇到过像 ({})
这样的语句,而且我在网上找不到任何解释。任何帮助将不胜感激,谢谢:)
上下文:
这是扩展以下代码中的宏时得到的代码:
#define SI ({int a;scanf("%d",&a);a;});
int x = SI;
这是某人在编程竞赛中使用的代码。
What does a statement such as this mean ?
int x = ( { int a; scanf( "%d", &a ); a ; } ) ;
It compiles and runs equivalent to :
int x;
scanf( "%d", &x );
It seems to be like some kind of anonymous function call or something, but I'm not sure. I have not come across statements like ({})
before, and I am unable to find any explanations online. Any help would be very much appreciated, thank you :)
Context:
This is the code that you get when the macros in the following code are expanded :
#define SI ({int a;scanf("%d",&a);a;});
int x = SI;
This is the code used by someone in a programming competition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是一个语句表达式。
它作为 GCC 支持的编译器扩展,它不是标准 C++,因此它是不可移植的。
如果您使用
-pedantic
标志编译代码,它会告诉您这一点。我的这个答案详细讨论了它。
It is an Statement Expression.
It as an compiler extension supported by GCC and it is not Standard C++,hence it is non portable.
If you compile your code with the
-pedantic
flag it will tell you so.This answer of mine talks about it in detail.