How does “()” convert statements into expressions in C++?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question























  • Almost surely duplicate of Are compound statements (blocks) surrounded by parens expressions in ANSI C?
    – Shafik Yaghmour
    1 hour ago














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?










share|improve this question























  • Almost surely duplicate of Are compound statements (blocks) surrounded by parens expressions in ANSI C?
    – Shafik Yaghmour
    1 hour ago












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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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.






share|improve this answer


















  • 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 a do ... while (0) loop.
    – cmaster
    2 hours ago










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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.






share|improve this answer


















  • 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 a do ... while (0) loop.
    – cmaster
    2 hours ago














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.






share|improve this answer


















  • 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 a do ... while (0) loop.
    – cmaster
    2 hours ago












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 a do ... while (0) loop.
    – cmaster
    2 hours ago












  • 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 a do ... 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery