简体   繁体   中英

Anonymous code blocks in c

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM