473,513 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 14 '05 #1
18 2432
Fredrik Tolf <fr*****@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) ks***@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.
Nov 14 '05 #2
"Fredrik Tolf" <fr*****@dolda2000.com> wrote in message
news:10***********************@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
Nov 14 '05 #3
Dag Viken <da*******@earthlink.net> scribbled the following:
"Fredrik Tolf" <fr*****@dolda2000.com> wrote in message
news:10***********************@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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Nothing lasts forever - so why not destroy it now?"
- Quake
Nov 14 '05 #4

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cj**********@oravannahka.helsinki.fi...
Dag Viken <da*******@earthlink.net> scribbled the following:
"Fredrik Tolf" <fr*****@dolda2000.com> wrote in message
news:10***********************@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?

Nov 14 '05 #5
Dag Viken <da*******@earthlink.net> scribbled the following:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cj**********@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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++. C++ run. Run, ++, run."
- JIPsoft
Nov 14 '05 #6
"Dag Viken" <da*******@earthlink.net> wrote:
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cj**********@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
Nov 14 '05 #7
Richard Bos wrote:
"Dag Viken" <da*******@earthlink.net> wrote:
"Joona I Palaste" <pa*****@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?
Nov 14 '05 #8
In <Jm*****************@newsread3.news.atl.earthlink. net> "Dag Viken" <da*******@earthlink.net> writes:

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cj**********@oravannahka.helsinki.fi...
Dag Viken <da*******@earthlink.net> scribbled the following:
> "Fredrik Tolf" <fr*****@dolda2000.com> wrote in message
> news:10***********************@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 intendedto >> 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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #9
On Thu, 2004-09-30 at 03:08 +0000, Keith Thompson wrote:
Fredrik Tolf <fr*****@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
Nov 14 '05 #10
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-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #11
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
Nov 14 '05 #12
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-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #13
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> 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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #14
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.

--
Er*********@sun.com

Nov 14 '05 #15
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
Nov 14 '05 #16
On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> 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. ;-)
Nov 14 '05 #17
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:
On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> 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: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #18
On Thu, 2004-09-30 at 16:10 +0000, Dan Pop wrote:
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> writes:
On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
In <10***********************@pc7.dolda2000.com> Fredrik Tolf <fr*****@dolda2000.com> 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. ;-)
Nov 14 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2600
by: Khaled Afiouni | last post by:
Hello, How can I stop/prevent SQL server from running compound SQL statements. I do not want the server to run multiple update/delete/insert/select statements as a batch. Is there an option? ...
3
1556
by: andrewbb | last post by:
Is it possible to force the use of a compound index in a query? create table Test (ColOne int, ColTwo int) The compound index is ColOne + ColTwo. I'm interested in searching on ColTwo, but I...
3
3410
by: Mantorok Redgormor | last post by:
What is the point of them? When should I use them? They seem to be rather useless but hopefully someone can prove me wrong. I think they are just additions to c99 but didn't exist in c89/90....
12
1853
by: Martin Johansen | last post by:
In C, what do you call that which is separated by semicolon? what is the difference between an expression and a statement? Thank you.
2
3947
by: Top Gun | last post by:
I am trying to code for a compound conditional in the DataView.RowFilter but don't quite no how to do the correct syntax for this. The following code sample chokes on dv.RowFilter: int batchid...
20
1991
by: Neroku | last post by:
Hello, i would like to know what the serious definition of statements and expressions is: i know an expression are evaluated to a value, i.e: 1 == 2 5+7 foo( 1,2) and a statement is...
11
7375
by: db2admin | last post by:
hello, is it possible to write compound sql without stored procedure or trigger. can i just run in command center of db2. regards, jagdip singh
9
1424
by: Jameson.Quinn | last post by:
I have: try: for line in open(myFileName): count += 1 except IOError: print "Can't open myfile" (I know, this is bad, I never close the file, but its just for illustration). But then I...
0
1269
by: Neil Cerutti | last post by:
The docs say: A suite can be one or more semicolon-separated simple statements on the same line as the header, following the header's colon, or it can be one or more indented statements on...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.