How does “()†convert statements into expressions in C++?
Clash Royale CLAN TAG#URR8PPP
up vote
12
down vote
favorite
I have the following code:
int main()
int i=0;
int j=(int k=3;++i;)+1; // this line
return 0;
It compiles and runs. If I remove the ()
from "this line", then it doesn't compile.
I'm just curious what syntax rule is being applied here.
The contains 2 statements, and the last statement indicates the "return" value of this code block. Then why does it need an extra
()
pair to make this return value usable?
c++ return expression block
add a comment |Â
up vote
12
down vote
favorite
I have the following code:
int main()
int i=0;
int j=(int k=3;++i;)+1; // this line
return 0;
It compiles and runs. If I remove the ()
from "this line", then it doesn't compile.
I'm just curious what syntax rule is being applied here.
The contains 2 statements, and the last statement indicates the "return" value of this code block. Then why does it need an extra
()
pair to make this return value usable?
c++ return expression block
Almost surely duplicate of Are compound statements (blocks) surrounded by parens expressions in ANSI C?
– Shafik Yaghmour
1 hour ago
add a comment |Â
up vote
12
down vote
favorite
up vote
12
down vote
favorite
I have the following code:
int main()
int i=0;
int j=(int k=3;++i;)+1; // this line
return 0;
It compiles and runs. If I remove the ()
from "this line", then it doesn't compile.
I'm just curious what syntax rule is being applied here.
The contains 2 statements, and the last statement indicates the "return" value of this code block. Then why does it need an extra
()
pair to make this return value usable?
c++ return expression block
I have the following code:
int main()
int i=0;
int j=(int k=3;++i;)+1; // this line
return 0;
It compiles and runs. If I remove the ()
from "this line", then it doesn't compile.
I'm just curious what syntax rule is being applied here.
The contains 2 statements, and the last statement indicates the "return" value of this code block. Then why does it need an extra
()
pair to make this return value usable?
c++ return expression block
c++ return expression block
edited 2 hours ago


scopchanov
2,07331436
2,07331436
asked 5 hours ago
Hind Forsum
2,26131534
2,26131534
Almost surely duplicate of Are compound statements (blocks) surrounded by parens expressions in ANSI C?
– Shafik Yaghmour
1 hour ago
add a comment |Â
Almost surely duplicate of Are compound statements (blocks) surrounded by parens expressions in ANSI C?
– Shafik Yaghmour
1 hour ago
Almost surely duplicate of Are compound statements (blocks) surrounded by parens expressions in ANSI C?
– Shafik Yaghmour
1 hour ago
Almost surely duplicate of Are compound statements (blocks) surrounded by parens expressions in ANSI C?
– Shafik Yaghmour
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
25
down vote
accepted
That's a statement expression, and it's a GCC-specific extension.
From the linked reference:
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.
A compound statement is a curly-brace enclosed block of statements.
6
Not to be confused with an expression statement, which is a standard thing.
– user2079303
4 hours ago
1
With not a lot more furniture you could probably use a c++11 standard lambda expression to the same effect.
– Gem Taylor
4 hours ago
I doubt the link will die but would you mind quoting what it points to in your answer so you can get the information all in one place?
– NathanOliver
4 hours ago
@GemTaylor for that reason I think most C++ compilers don't support this extension: there's no point adding it to the language now, so it's pretty much deprecated in GNU C++. For C, the situation is more flexible as no lambda syntax has been standardized yet so this extension is more useful there. As the wording of the documentation hints, GCC expects this extension to be used mostly from C, not so much from C++; it's available from C++ anyway because G++ is permissive about that kind of thing.
– Leushenko
2 hours ago
1
Adding to what @Leushenko wrote: In C, statement expressions are dead useful for writing function-call like macros. It allows the macro to define its own variables in its own scope, and still "return" a value from the macro. The later is not possible with the other common workaround, which encloses the macro body in ado ... while (0)
loop.
– cmaster
2 hours ago
 |Â
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
25
down vote
accepted
That's a statement expression, and it's a GCC-specific extension.
From the linked reference:
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.
A compound statement is a curly-brace enclosed block of statements.
6
Not to be confused with an expression statement, which is a standard thing.
– user2079303
4 hours ago
1
With not a lot more furniture you could probably use a c++11 standard lambda expression to the same effect.
– Gem Taylor
4 hours ago
I doubt the link will die but would you mind quoting what it points to in your answer so you can get the information all in one place?
– NathanOliver
4 hours ago
@GemTaylor for that reason I think most C++ compilers don't support this extension: there's no point adding it to the language now, so it's pretty much deprecated in GNU C++. For C, the situation is more flexible as no lambda syntax has been standardized yet so this extension is more useful there. As the wording of the documentation hints, GCC expects this extension to be used mostly from C, not so much from C++; it's available from C++ anyway because G++ is permissive about that kind of thing.
– Leushenko
2 hours ago
1
Adding to what @Leushenko wrote: In C, statement expressions are dead useful for writing function-call like macros. It allows the macro to define its own variables in its own scope, and still "return" a value from the macro. The later is not possible with the other common workaround, which encloses the macro body in ado ... while (0)
loop.
– cmaster
2 hours ago
 |Â
show 1 more comment
up vote
25
down vote
accepted
That's a statement expression, and it's a GCC-specific extension.
From the linked reference:
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.
A compound statement is a curly-brace enclosed block of statements.
6
Not to be confused with an expression statement, which is a standard thing.
– user2079303
4 hours ago
1
With not a lot more furniture you could probably use a c++11 standard lambda expression to the same effect.
– Gem Taylor
4 hours ago
I doubt the link will die but would you mind quoting what it points to in your answer so you can get the information all in one place?
– NathanOliver
4 hours ago
@GemTaylor for that reason I think most C++ compilers don't support this extension: there's no point adding it to the language now, so it's pretty much deprecated in GNU C++. For C, the situation is more flexible as no lambda syntax has been standardized yet so this extension is more useful there. As the wording of the documentation hints, GCC expects this extension to be used mostly from C, not so much from C++; it's available from C++ anyway because G++ is permissive about that kind of thing.
– Leushenko
2 hours ago
1
Adding to what @Leushenko wrote: In C, statement expressions are dead useful for writing function-call like macros. It allows the macro to define its own variables in its own scope, and still "return" a value from the macro. The later is not possible with the other common workaround, which encloses the macro body in ado ... while (0)
loop.
– cmaster
2 hours ago
 |Â
show 1 more comment
up vote
25
down vote
accepted
up vote
25
down vote
accepted
That's a statement expression, and it's a GCC-specific extension.
From the linked reference:
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.
A compound statement is a curly-brace enclosed block of statements.
That's a statement expression, and it's a GCC-specific extension.
From the linked reference:
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.
A compound statement is a curly-brace enclosed block of statements.
edited 4 hours ago
answered 4 hours ago


Some programmer dude
282k23231385
282k23231385
6
Not to be confused with an expression statement, which is a standard thing.
– user2079303
4 hours ago
1
With not a lot more furniture you could probably use a c++11 standard lambda expression to the same effect.
– Gem Taylor
4 hours ago
I doubt the link will die but would you mind quoting what it points to in your answer so you can get the information all in one place?
– NathanOliver
4 hours ago
@GemTaylor for that reason I think most C++ compilers don't support this extension: there's no point adding it to the language now, so it's pretty much deprecated in GNU C++. For C, the situation is more flexible as no lambda syntax has been standardized yet so this extension is more useful there. As the wording of the documentation hints, GCC expects this extension to be used mostly from C, not so much from C++; it's available from C++ anyway because G++ is permissive about that kind of thing.
– Leushenko
2 hours ago
1
Adding to what @Leushenko wrote: In C, statement expressions are dead useful for writing function-call like macros. It allows the macro to define its own variables in its own scope, and still "return" a value from the macro. The later is not possible with the other common workaround, which encloses the macro body in ado ... while (0)
loop.
– cmaster
2 hours ago
 |Â
show 1 more comment
6
Not to be confused with an expression statement, which is a standard thing.
– user2079303
4 hours ago
1
With not a lot more furniture you could probably use a c++11 standard lambda expression to the same effect.
– Gem Taylor
4 hours ago
I doubt the link will die but would you mind quoting what it points to in your answer so you can get the information all in one place?
– NathanOliver
4 hours ago
@GemTaylor for that reason I think most C++ compilers don't support this extension: there's no point adding it to the language now, so it's pretty much deprecated in GNU C++. For C, the situation is more flexible as no lambda syntax has been standardized yet so this extension is more useful there. As the wording of the documentation hints, GCC expects this extension to be used mostly from C, not so much from C++; it's available from C++ anyway because G++ is permissive about that kind of thing.
– Leushenko
2 hours ago
1
Adding to what @Leushenko wrote: In C, statement expressions are dead useful for writing function-call like macros. It allows the macro to define its own variables in its own scope, and still "return" a value from the macro. The later is not possible with the other common workaround, which encloses the macro body in ado ... while (0)
loop.
– cmaster
2 hours ago
6
6
Not to be confused with an expression statement, which is a standard thing.
– user2079303
4 hours ago
Not to be confused with an expression statement, which is a standard thing.
– user2079303
4 hours ago
1
1
With not a lot more furniture you could probably use a c++11 standard lambda expression to the same effect.
– Gem Taylor
4 hours ago
With not a lot more furniture you could probably use a c++11 standard lambda expression to the same effect.
– Gem Taylor
4 hours ago
I doubt the link will die but would you mind quoting what it points to in your answer so you can get the information all in one place?
– NathanOliver
4 hours ago
I doubt the link will die but would you mind quoting what it points to in your answer so you can get the information all in one place?
– NathanOliver
4 hours ago
@GemTaylor for that reason I think most C++ compilers don't support this extension: there's no point adding it to the language now, so it's pretty much deprecated in GNU C++. For C, the situation is more flexible as no lambda syntax has been standardized yet so this extension is more useful there. As the wording of the documentation hints, GCC expects this extension to be used mostly from C, not so much from C++; it's available from C++ anyway because G++ is permissive about that kind of thing.
– Leushenko
2 hours ago
@GemTaylor for that reason I think most C++ compilers don't support this extension: there's no point adding it to the language now, so it's pretty much deprecated in GNU C++. For C, the situation is more flexible as no lambda syntax has been standardized yet so this extension is more useful there. As the wording of the documentation hints, GCC expects this extension to be used mostly from C, not so much from C++; it's available from C++ anyway because G++ is permissive about that kind of thing.
– Leushenko
2 hours ago
1
1
Adding to what @Leushenko wrote: In C, statement expressions are dead useful for writing function-call like macros. It allows the macro to define its own variables in its own scope, and still "return" a value from the macro. The later is not possible with the other common workaround, which encloses the macro body in a
do ... while (0)
loop.– cmaster
2 hours ago
Adding to what @Leushenko wrote: In C, statement expressions are dead useful for writing function-call like macros. It allows the macro to define its own variables in its own scope, and still "return" a value from the macro. The later is not possible with the other common workaround, which encloses the macro body in a
do ... while (0)
loop.– cmaster
2 hours ago
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52313218%2fhow-does-convert-statements-into-expressions-in-c%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Almost surely duplicate of Are compound statements (blocks) surrounded by parens expressions in ANSI C?
– Shafik Yaghmour
1 hour ago