Compound statements in expressions

Discussion in 'C Programming' started by Fredrik Tolf, Sep 30, 2004.

  1. Fredrik Tolf

    Fredrik Tolf Guest

    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
    Fredrik Tolf, Sep 30, 2004
    #1
    1. Advertising

  2. Fredrik Tolf <> 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) <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.
    Keith Thompson, Sep 30, 2004
    #2
    1. Advertising

  3. Fredrik Tolf

    Dag Viken Guest

    "Fredrik Tolf" <> wrote in message
    news:2000.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
    Dag Viken, Sep 30, 2004
    #3
  4. Dag Viken <> scribbled the following:
    > "Fredrik Tolf" <> wrote in message
    > news:2000.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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Nothing lasts forever - so why not destroy it now?"
    - Quake
    Joona I Palaste, Sep 30, 2004
    #4
  5. Fredrik Tolf

    Dag Viken Guest

    "Joona I Palaste" <> wrote in message
    news:cjgaau$4ja$...
    > Dag Viken <> scribbled the following:
    > > "Fredrik Tolf" <> wrote in message
    > > news:2000.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?
    Dag Viken, Sep 30, 2004
    #5
  6. Dag Viken <> scribbled the following:
    > "Joona I Palaste" <> wrote in message
    > news:cjgaau$4ja$...
    >> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "C++. C++ run. Run, ++, run."
    - JIPsoft
    Joona I Palaste, Sep 30, 2004
    #6
  7. Fredrik Tolf

    Richard Bos Guest

    "Dag Viken" <> wrote:

    > "Joona I Palaste" <> wrote in message
    > news:cjgaau$4ja$...
    > > 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
    Richard Bos, Sep 30, 2004
    #7
  8. Fredrik Tolf

    CBFalconer Guest

    Richard Bos wrote:
    > "Dag Viken" <> wrote:
    >> "Joona I Palaste" <> 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?
    CBFalconer, Sep 30, 2004
    #8
  9. Fredrik Tolf

    Dan Pop Guest

    In <JmO6d.5204$> "Dag Viken" <> writes:


    >"Joona I Palaste" <> wrote in message
    >news:cjgaau$4ja$...
    >> Dag Viken <> scribbled the following:
    >> > "Fredrik Tolf" <> wrote in message
    >> > news:2000.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:
    Currently looking for a job in the European Union
    Dan Pop, Sep 30, 2004
    #9
  10. Fredrik Tolf

    Fredrik Tolf Guest

    On Thu, 2004-09-30 at 03:08 +0000, Keith Thompson wrote:
    > Fredrik Tolf <> 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
    Fredrik Tolf, Sep 30, 2004
    #10
  11. Fredrik Tolf

    Chris Dollin Guest

    Fredrik Tolf wrote:

    > 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?


    It's a straightforward (but stupid) use of the comma operator.

    E1, E2 [where the comma is *not* an argument separator]

    means "evaluate E1, throw away the result, evaluate E2 and return its
    result as the result of the entire expression".

    --
    Chris "electric hedgehog" Dollin
    C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    Chris Dollin, Sep 30, 2004
    #11
  12. Fredrik Tolf

    Fredrik Tolf Guest

    On Thu, 2004-09-30 at 13:41 +0100, Chris Dollin wrote:
    > Fredrik Tolf wrote:
    >
    > > 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?

    >
    > It's a straightforward (but stupid) use of the comma operator.
    >
    > E1, E2 [where the comma is *not* an argument separator]
    >
    > means "evaluate E1, throw away the result, evaluate E2 and return its
    > result as the result of the entire expression".


    I see. Am I correct if I say that declarations cannot be part of comma-
    separated statements?

    Fredrik Tolf
    Fredrik Tolf, Sep 30, 2004
    #12
  13. Fredrik Tolf

    Chris Dollin Guest

    Fredrik Tolf wrote:

    > On Thu, 2004-09-30 at 13:41 +0100, Chris Dollin wrote:
    >> Fredrik Tolf wrote:
    >>
    >> > 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?

    >>
    >> It's a straightforward (but stupid) use of the comma operator.
    >>
    >> E1, E2 [where the comma is *not* an argument separator]
    >>
    >> means "evaluate E1, throw away the result, evaluate E2 and return its
    >> result as the result of the entire expression".

    >
    > I see. Am I correct if I say that declarations cannot be part of comma-
    > separated statements?


    Yes, but only because there aren't any comma-separated statements.
    The comma-expression's operands are expressions, not statements.

    [Assignments are, technically, expressions, although of course they're
    very statement-like things, since they're primarily executed for effect
    not value. But I'm picking the syntactic definition for statements
    here, since the semantic one isn't so useful.]

    --
    Chris "electric hedgehog" Dollin
    C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
    Chris Dollin, Sep 30, 2004
    #13
  14. Fredrik Tolf

    Dan Pop Guest

    In <2000.com> Fredrik Tolf <> writes:

    >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?


    Ever considered reading a C book?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
    Dan Pop, Sep 30, 2004
    #14
  15. Fredrik Tolf

    Eric Sosman Guest

    Fredrik Tolf wrote:
    > On Thu, 2004-09-30 at 13:41 +0100, Chris Dollin wrote:
    >
    >>Fredrik Tolf wrote:
    >>
    >>
    >>>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?

    >>
    >>It's a straightforward (but stupid) use of the comma operator.
    >>
    >> E1, E2 [where the comma is *not* an argument separator]
    >>
    >>means "evaluate E1, throw away the result, evaluate E2 and return its
    >>result as the result of the entire expression".

    >
    >
    > I see. Am I correct if I say that declarations cannot be part of comma-
    > separated statements?


    There are no "comma-separated statements" in C. The
    examples above are *expressions* using the comma *operator*,
    whose two operands are themselves sub-expressions. Would
    you call `a / b' a "slash-separated statement?" This may
    seem like pointless pedantry at first sight, and maybe even
    at second or third sight. However, it contains the answer to
    your immediate question, and perhaps to many other questions
    that might occur to you later.

    Rephrased, the question becomes "Can declarations appear
    in expressions?" For the current C Standard, the answer is
    "No," unequivocally. For the now-obsolete former Standard,
    the answer is "Only in one special case," namely, that the
    invocation of a previously undeclared function serves as a
    declaration of that function. If f() and g() have not been
    declared,

    a + f()
    b , g()

    are expressions that are erroneous under the current Standard,
    but that implicitly declare f() and g() as functions returning
    `int' values under the obsolete Standard. No other kinds of
    declarations can appear in expressions, under either Standard.

    --
    Eric Sosman, Sep 30, 2004
    #15
  16. Fredrik Tolf

    Fredrik Tolf Guest

    On Thu, 2004-09-30 at 15:27 +0100, Chris Dollin wrote:
    > Fredrik Tolf wrote:
    >
    > > On Thu, 2004-09-30 at 13:41 +0100, Chris Dollin wrote:
    > >> Fredrik Tolf wrote:
    > >>
    > >> > 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?
    > >>
    > >> It's a straightforward (but stupid) use of the comma operator.
    > >>
    > >> E1, E2 [where the comma is *not* an argument separator]
    > >>
    > >> means "evaluate E1, throw away the result, evaluate E2 and return its
    > >> result as the result of the entire expression".

    > >
    > > I see. Am I correct if I say that declarations cannot be part of comma-
    > > separated statements?

    >
    > Yes, but only because there aren't any comma-separated statements.
    > The comma-expression's operands are expressions, not statements.


    Oh, that makes it make sense. Actually, I should have seen that, but oh
    well...

    Thanks for your time!

    Fredrik Tolf
    Fredrik Tolf, Sep 30, 2004
    #16
  17. Fredrik Tolf

    Fredrik Tolf Guest

    On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
    > In <2000.com> Fredrik Tolf <> writes:
    >
    > >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?

    >
    > Ever considered reading a C book?


    Nope -- those tend to be expensive. ;-)
    Fredrik Tolf, Sep 30, 2004
    #17
  18. Fredrik Tolf

    Dan Pop Guest

    In <2000.com> Fredrik Tolf <> writes:

    >On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
    >> In <2000.com> Fredrik Tolf <> writes:
    >>
    >> >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?

    >>
    >> Ever considered reading a C book?

    >
    >Nope -- those tend to be expensive. ;-)


    While our time is a free resource ;-(

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
    Dan Pop, Sep 30, 2004
    #18
  19. Fredrik Tolf

    Fredrik Tolf Guest

    OT: Re: Compound statements in expressions

    On Thu, 2004-09-30 at 16:10 +0000, Dan Pop wrote:
    > In <2000.com> Fredrik Tolf <> writes:
    >
    > >On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
    > >> In <2000.com> Fredrik Tolf <> writes:
    > >>
    > >> >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?
    > >>
    > >> Ever considered reading a C book?

    > >
    > >Nope -- those tend to be expensive. ;-)

    >
    > While our time is a free resource ;-(


    Not that I don't appreciate it, but in terms of money-free -- yes, it
    is. ;-)
    Fredrik Tolf, Sep 30, 2004
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,157
    mfmehdi
    Oct 20, 2006
  2. Replies:
    3
    Views:
    849
    Steve Holden
    Sep 16, 2003
  3. Neil Cerutti

    Nested compound statements.

    Neil Cerutti, Feb 1, 2008, in forum: Python
    Replies:
    0
    Views:
    321
    Neil Cerutti
    Feb 1, 2008
  4. Kurt Euler
    Replies:
    1
    Views:
    160
    Jason Creighton
    Sep 3, 2003
  5. Randy Kramer
    Replies:
    11
    Views:
    349
    Randy Kramer
    Oct 19, 2007
Loading...

Share This Page