![]() |
Compound statements in expressions
Take a look at this C snippet:
#include <stdio.h> int test(int *var) { return(*var += 5); } int main(void) { int var; var = 1; printf("%i %i %i\n", ({var += 4; var;}), (printf("%i\n", test(&var)), var), (var += 10, var -= 4)); } When compiled with GCC 3.3, it outputs the following when run: 6 10 16 16 Now I'm wondering - Are these types of constructs allowed by the C standard, or are they GCC extensions? If they are standardized, what is the standardized semantics. That is, in which order are they intended to be executed, what's the difference between "(var += 4, var)" and "({var +=4; var;})", and what does "(expr1, expr2, expr3)" really mean when it's not an argument list to a function. Also, why must I write "({var += 4; var;})" - Why can't I skip the outer parentheses? Thanks for your attention! Fredrik Tolf |
Re: Compound statements in expressions
Fredrik Tolf <fredrik@dolda2000.com> writes:
> Take a look at this C snippet: > > #include <stdio.h> > > int test(int *var) > { > return(*var += 5); > } > > int main(void) > { > int var; > > var = 1; > printf("%i %i %i\n", ({var += 4; var;}), > (printf("%i\n", test(&var)), var), > (var += 10, var -= 4)); > } > > When compiled with GCC 3.3, it outputs the following when run: > 6 > 10 16 16 > > Now I'm wondering - Are these types of constructs allowed by the C > standard, or are they GCC extensions? It's a gcc extension. <OT> If your system supports it, try info gcc 'c extensions' 'statement exprs' </OT> -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this. |
Re: Compound statements in expressions
"Fredrik Tolf" <fredrik@dolda2000.com> wrote in message
news:1096512695.10848.63.camel@pc7.dolda2000.com.. . > Take a look at this C snippet: > > #include <stdio.h> > > int test(int *var) > { > return(*var += 5); > } > > int main(void) > { > int var; > > var = 1; > printf("%i %i %i\n", ({var += 4; var;}), > (printf("%i\n", test(&var)), var), > (var += 10, var -= 4)); > } > > When compiled with GCC 3.3, it outputs the following when run: > 6 > 10 16 16 > > Now I'm wondering - Are these types of constructs allowed by the C > standard, or are they GCC extensions? If they are standardized, what is > the standardized semantics. That is, in which order are they intended to > be executed, what's the difference between "(var += 4, var)" and "({var > +=4; var;})", and what does "(expr1, expr2, expr3)" really mean when > it's not an argument list to a function. > > Also, why must I write "({var += 4; var;})" - Why can't I skip the outer > parentheses? > > Thanks for your attention! > > Fredrik Tolf > > This does not compile on my system - i.e. VC7 with {} inside (). I do not think it is valid code. Dag |
Re: Compound statements in expressions
Dag Viken <dag.viken@earthlink.net> scribbled the following:
> "Fredrik Tolf" <fredrik@dolda2000.com> wrote in message > news:1096512695.10848.63.camel@pc7.dolda2000.com.. . >> Take a look at this C snippet: >> >> #include <stdio.h> >> >> int test(int *var) >> { >> return(*var += 5); >> } >> >> int main(void) >> { >> int var; >> >> var = 1; >> printf("%i %i %i\n", ({var += 4; var;}), >> (printf("%i\n", test(&var)), var), >> (var += 10, var -= 4)); >> } >> >> When compiled with GCC 3.3, it outputs the following when run: >> 6 >> 10 16 16 >> >> Now I'm wondering - Are these types of constructs allowed by the C >> standard, or are they GCC extensions? If they are standardized, what is >> the standardized semantics. That is, in which order are they intended to >> be executed, what's the difference between "(var += 4, var)" and "({var >> +=4; var;})", and what does "(expr1, expr2, expr3)" really mean when >> it's not an argument list to a function. > This does not compile on my system - i.e. VC7 with {} inside (). > I do not think it is valid code. As has already been said, it's a GCC extension. So it's non-standard code and thus not guaranteed to be valid on *any* specific compiler. -- /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "Nothing lasts forever - so why not destroy it now?" - Quake |
Re: Compound statements in expressions
"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message news:cjgaau$4ja$1@oravannahka.helsinki.fi... > Dag Viken <dag.viken@earthlink.net> scribbled the following: > > "Fredrik Tolf" <fredrik@dolda2000.com> wrote in message > > news:1096512695.10848.63.camel@pc7.dolda2000.com.. . [snip] > >> > >> Now I'm wondering - Are these types of constructs allowed by the C > >> standard, or are they GCC extensions? If they are standardized, what is > >> the standardized semantics. That is, in which order are they intended to > >> be executed, what's the difference between "(var += 4, var)" and "({var > >> +=4; var;})", and what does "(expr1, expr2, expr3)" really mean when > >> it's not an argument list to a function. > > > This does not compile on my system - i.e. VC7 with {} inside (). > > I do not think it is valid code. > > As has already been said, it's a GCC extension. So it's non-standard > code and thus not guaranteed to be valid on *any* specific compiler. Why make extensions that are incompatible with the existing C/C++ standards? It certainly does not encourage source code compatibility. And especially if it is gcc - is there an upcoming standard that supports these extended features? |
Re: Compound statements in expressions
Dag Viken <dag.viken@earthlink.net> scribbled the following:
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message > news:cjgaau$4ja$1@oravannahka.helsinki.fi... >> As has already been said, it's a GCC extension. So it's non-standard >> code and thus not guaranteed to be valid on *any* specific compiler. > Why make extensions that are incompatible with the existing C/C++ standards? > It certainly does not encourage source code compatibility. And especially if > it is gcc - is there an upcoming standard that supports these extended > features? That's what I've been wondering too. Maybe you can ask at gnu.gcc.help or something? -- /-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\ \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ "C++. C++ run. Run, ++, run." - JIPsoft |
Re: Compound statements in expressions
"Dag Viken" <dag.viken@earthlink.net> wrote:
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message > news:cjgaau$4ja$1@oravannahka.helsinki.fi... > > As has already been said, it's a GCC extension. So it's non-standard > > code and thus not guaranteed to be valid on *any* specific compiler. > > Why make extensions that are incompatible with the existing C/C++ standards? > It certainly does not encourage source code compatibility. And especially if > it is gcc - is there an upcoming standard that supports these extended > features? You assume that GNU _wants_ source code compatibility. IYAM, they are second only to M$ for embrace-and-extend, and one cannot help but suspect that this is quite intentional. Richard |
Re: Compound statements in expressions
Richard Bos wrote:
> "Dag Viken" <dag.viken@earthlink.net> wrote: >> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message >> >>> As has already been said, it's a GCC extension. So it's >>> non-standard code and thus not guaranteed to be valid on *any* >>> specific compiler. >> >> Why make extensions that are incompatible with the existing >> C/C++ standards? It certainly does not encourage source code >> compatibility. And especially if it is gcc - is there an >> upcoming standard that supports these extended features? > > You assume that GNU _wants_ source code compatibility. IYAM, > they are second only to M$ for embrace-and-extend, and one > cannot help but suspect that this is quite intentional. It is your own fault for not using it with the -ansi -pedantic switches. Unlike M$ gcc allows all those extensions to be switched off. I consider it would be preferable to allow them to be switched on. Then you can think of gcc as a test bed for possible new features. -- A: Because it fouls the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? |
Re: Compound statements in expressions
In <JmO6d.5204$ls6.2061@newsread3.news.atl.earthlink. net> "Dag Viken" <dag.viken@earthlink.net> writes:
>"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message >news:cjgaau$4ja$1@oravannahka.helsinki.fi... >> Dag Viken <dag.viken@earthlink.net> scribbled the following: >> > "Fredrik Tolf" <fredrik@dolda2000.com> wrote in message >> > news:1096512695.10848.63.camel@pc7.dolda2000.com.. . >[snip] >> >> >> >> Now I'm wondering - Are these types of constructs allowed by the C >> >> standard, or are they GCC extensions? You can trivially get the answer yourself, by compiling with gcc in standard conforming mode (the -ansi -pedantic options). If you get a clean compile, they are probably allowed by the C standard (the other possibility is that they invoke undefined behaviour, so no diagnostic is require). >> >> the standardized semantics. That is, in which order are they intended >to >> >> be executed, what's the difference between "(var += 4, var)" and "({var >> >> +=4; var;})", and what does "(expr1, expr2, expr3)" really mean when >> >> it's not an argument list to a function. >> >> > This does not compile on my system - i.e. VC7 with {} inside (). >> > I do not think it is valid code. >> >> As has already been said, it's a GCC extension. So it's non-standard >> code and thus not guaranteed to be valid on *any* specific compiler. > >Why make extensions that are incompatible with the existing C/C++ standards? Why not, as long as they don't break *any* correct C program? >It certainly does not encourage source code compatibility. If the extension is useful, it becomes existing practice for a future revision of the C standard. That is, assuming that the standardisation committee is looking for existing practice in the right places... Most of the gcc extensions are far from gratuitous and it is a pity that the C standardisation process has ignored/rejected them. The one you're talking about greatly improves the functionality of function-like macros, especially when used together with another gcc extension: typeof. >And especially if >it is gcc - is there an upcoming standard that supports these extended >features? For the time being, there is no upcoming C standard. Merely bug fixes to the current standard. Dan -- Dan Pop DESY Zeuthen, RZ group Email: Dan.Pop@ifh.de Currently looking for a job in the European Union |
Re: Compound statements in expressions
On Thu, 2004-09-30 at 03:08 +0000, Keith Thompson wrote:
> Fredrik Tolf <fredrik@dolda2000.com> writes: > > Take a look at this C snippet: > > > > #include <stdio.h> > > > > int test(int *var) > > { > > return(*var += 5); > > } > > > > int main(void) > > { > > int var; > > > > var = 1; > > printf("%i %i %i\n", ({var += 4; var;}), > > (printf("%i\n", test(&var)), var), > > (var += 10, var -= 4)); > > } > > > > When compiled with GCC 3.3, it outputs the following when run: > > 6 > > 10 16 16 > > > > Now I'm wondering - Are these types of constructs allowed by the C > > standard, or are they GCC extensions? > > It's a gcc extension. > > <OT> > If your system supports it, try > info gcc 'c extensions' 'statement exprs' > </OT> I see. I guess I should have tried -ansi, but I didn't think of it. However, that texinfo only mentions these ({...}) constructs. How about this: printf("%i\n", (var += 10, var -= 4)); GCC accepts that with -Wall -pedantic -ansi, so I guess that is standard C. But again, what does it really mean? Fredrik Tolf |
| All times are GMT. The time now is 11:32 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.