Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compound statements in expressions

Reply
Thread Tools

Compound statements in expressions

 
 
Chris Dollin
Guest
Posts: n/a
 
      09-30-2004
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
 
Reply With Quote
 
 
 
 
Fredrik Tolf
Guest
Posts: n/a
 
      09-30-2004
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


 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      09-30-2004
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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      09-30-2004
In <(E-Mail Removed)2000.com> Fredrik Tolf <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-30-2004
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.

--
(E-Mail Removed)

 
Reply With Quote
 
Fredrik Tolf
Guest
Posts: n/a
 
      09-30-2004
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


 
Reply With Quote
 
Fredrik Tolf
Guest
Posts: n/a
 
      09-30-2004
On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
> In <(E-Mail Removed)2000.com> Fredrik Tolf <(E-Mail Removed)> 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.


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      09-30-2004
In <(E-Mail Removed)2000.com> Fredrik Tolf <(E-Mail Removed)> writes:

>On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
>> In <(E-Mail Removed)2000.com> Fredrik Tolf <(E-Mail Removed)> 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: (E-Mail Removed)
Currently looking for a job in the European Union
 
Reply With Quote
 
Fredrik Tolf
Guest
Posts: n/a
 
      09-30-2004
On Thu, 2004-09-30 at 16:10 +0000, Dan Pop wrote:
> In <(E-Mail Removed)2000.com> Fredrik Tolf <(E-Mail Removed)> writes:
>
> >On Thu, 2004-09-30 at 14:35 +0000, Dan Pop wrote:
> >> In <(E-Mail Removed)2000.com> Fredrik Tolf <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested compound statements. Neil Cerutti Python 0 02-01-2008 07:03 PM
Compound conditionals in case when statements? Syntax? Randy Kramer Ruby 11 10-19-2007 11:36 AM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
compound conditional statements beliavsky@aol.com Python 3 09-15-2003 11:12 PM
Possible to make compound if statements... Kurt Euler Ruby 1 09-03-2003 05:11 PM



Advertisments