Compound statements in expressions

From: Fredrik Tolf (fredrik_at_dolda2000.com)
Date: 09/30/04


Date: Thu, 30 Sep 2004 04:51:34 +0200

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



Relevant Pages

  • Re: gcc, aliasing rules and unions
    ... If the standard dit not allow it, 'gcc -O2' would ... struct B {int x, y;}; ...
    (comp.lang.c)
  • Re: Does C have function to get angle?
    ... Behaviour dictated by the standard ... > int main{ ... > on an Intel machine using gcc with -O3, ... > print nothing on that same machine using gcc without ...
    (comp.lang.c)
  • Re: inclusione file
    ... another file without including its declaration] ... assumed that it is a function returning an int, as if you had declared it ... gcc only emits all required diagnostics if ... In 1999 a new version of the C standard was adopted, ...
    (comp.lang.c)
  • Re: Compound statements in expressions
    ... "Fredrik Tolf" wrote in message ... > int test ... > standard, ...
    (comp.lang.c)
  • Re: Does C have function to get angle?
    ... Behaviour dictated by the standard ... x+0.5 comparing greater than or equal to 1 is not. ... > int main{ ... > print nothing on that same machine using gcc without ...
    (comp.lang.c)