Subject: GCC's statement expressions

From: Shlomi Fish <[email-masked]>
Date: Tue, 8 Jan 2002 13:45:28 +0200 (IST)

The following snippet compiles with gcc:

#include <stdio.h>

int main()
{
    int sum;

    sum = 100;

    sum += ({ int s = 0; int a; for(a=0;a<10;a++) { s+=a; } s;});

    printf("%i\n", sum);

    return 0;
}

It seems the ({ ... }) are the equivalent of do { } or eval { } in perl. I
always wanted such a feature in C, so it's nice to know gcc has it. Too
bad it is not standard.

Regards,

	Shlomi Fish

----------------------------------------------------------------------
Shlomi Fish        [email-masked]
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       [email-masked]

"Let's suppose you have a table with 2^n cups..."
"Wait a second - is n a natural number?"

---

From: Oleg Goldshmidt <[email-masked]>
Date: 08 Jan 2002 14:02:20 +0200

> always wanted such a feature in C, so it's nice to know gcc has it. Too

I don't know... For me, the summation over a begs to be abstracted
into a separate function. Do that, and the need for ({}) disappears,
at least in this case. Essentially, you inlined the function by hand.

It is a matter of style, and it is futile to argue about it, but the
line above is far too complicated to be considered readable [which is
not the same as saying "I cannot understand it" ;-)].

-- 
Oleg Goldshmidt | [email-masked] 
"If it ain't broken, it has not got enough features yet."

---

From: Shlomi Fish <[email-masked]>
Date: Tue, 8 Jan 2002 16:33:06 +0200 (IST)

> not the same as saying "I cannot understand it" ;-)].

I did not say it was. But this feature enables one to write macros that
perform complex multi-line calculations and then can be used as lvalues.

Regards,

	Shlomi Fish

> To unsubscribe from this group, send an email to:
> [email-masked]
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>

---

From: mulix <[email-masked]>
Date: Tue, 8 Jan 2002 16:34:09 +0200 (IST)

> perform complex multi-line calculations and then can be used as lvalues.

are you sure you mean lvalues?

ssuming you meant rvalues, why would one want to do it that way?

static inline int foo()
{
	return some_very_complex_expression;
}

suits me fine, and has all of the benefits of being a function, namely
type checking.

inline is a gcc extension as well, true, but it has the benefit of at
least following common language idiom. i think it's also a keyword
starting from C99.

is there any instance, not including an ioccc submission, where the form
you wrote above is better than an inline function?
-- 
mulix

http://vipe.technion.ac.il/~mulix/
http://syscalltrack.sf.net/

---

From: Shlomi Fish <[email-masked]>
Date: Wed, 9 Jan 2002 12:17:47 +0200 (IST)

> are you sure you mean lvalues?

Sorry, I meant rvalues.

> you wrote above is better than an inline function?

Not really that I can think of. However, I wish all ANSI C compilers had
at least one of those forms.

Regards,

	Shlomi Fish