This is a statement expression. It is a gcc extension and according to the documentation 6.1 Statements and Declarations in Expressions:
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.
so for this piece of code:
int a=({10;});
according to these rules the value will be 10
which will be assigned to a
.
This extension is one many gcc extensions used in the Linux kernel, although the linked article does not actually cover statement expressions, this kernel newbies FAQ entry explains some of the reasoning behind using statement expressions in the Linux kernel.
As the gcc document notes compiling with the -pedantic option will warn you when you are using a gcc extension.
int a = [10];
– Clipboardint a = 10;
– Kytheraint a = [10];
, gcc gives meerror: expected expression before ‘[’ token
. What did you expect it to do? – Kytherachar* s = ["grijesh"];
will work so I confused. Thanks! – Clipboardint a = {1};
andchar *s = {"grijesh"};
works @codepade my mistake used[]
instead of{}
. – Clipboard