Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about the *= (and similar) operator

Reply
Thread Tools

Question about the *= (and similar) operator

 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-13-2006
In the statement "a *= expression" is expression assumed to be
parenthesized ? For example if I write "a *= b+c" is this the same
as "a = a * (b+c)" or "a = a * b+c" ?

 
Reply With Quote
 
 
 
 
Chris McDonald
Guest
Posts: n/a
 
      06-13-2006
writes:

>In the statement "a *= expression" is expression assumed to be
>parenthesized ? For example if I write "a *= b+c" is this the same
>as "a = a * (b+c)" or "a = a * b+c" ?


Write a small C program and try it.
The learning will provide benefits.

--
Chris.
 
Reply With Quote
 
 
 
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-13-2006

Chris McDonald wrote:

> writes:
>
> >In the statement "a *= expression" is expression assumed to be
> >parenthesized ? For example if I write "a *= b+c" is this the same
> >as "a = a * (b+c)" or "a = a * b+c" ?

>
> Write a small C program and try it.
> The learning will provide benefits.
>


It wouldn't provide any more benefits than what I would get from
a reply here. Plus experiments are a dangerous way of learning
about C unless one knows beforehand that the behaviour being
investigated
is determined by the standard. If that's not the case then one will not
know if what he observes is standard or implementation defined or
undefined which just happened to work in a specific way under a
specific
compiler on a specific platform. For all I know , whether "expression"
is
parenthesized or not in "a *= expression" , is implementation defined
and
if one wants to achieve portability one should include parentheses in
the
code whenever the standard operator precedence does not give the
desired
result.

Spiros Bousbouras

 
Reply With Quote
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      06-13-2006
wrote:
> In the statement "a *= expression" is expression assumed to be
> parenthesized ? For example if I write "a *= b+c" is this the same
> as "a = a * (b+c)" or "a = a * b+c" ?
>


ISO C99 draft (N1124) §6.5.16.2:
A compound assignment of the form E1 op = E2 differs from the simple
assignment expression E1 = E1 op (E2) only in that the lvalue E1 is
evaluated only once.


--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-13-2006
In article < .com>,
<> wrote:

>In the statement "a *= expression" is expression assumed to be
>parenthesized ? For example if I write "a *= b+c" is this the same
>as "a = a * (b+c)" or "a = a * b+c" ?


The latter would only be plausible if *= was some kind of macro. If
you could define infix macros and did

#define-infix x*=y x=x*y

then a*=b+c might expand to a=a*b+c.

But C operators are not like macros. The only question is the
relative precedence of *= and +. It will either be

(a *= b) + c, which is equivalent to (a = a*b) + c,
or
a *= (b + c), which is equivalent to a = a * (b+c).

In fact, it's a = a * (b + c), because the assignment operators all
have lower precedence than the arithmetic operators.

-- Richard
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-13-2006
In article <e6nho5$ntu$>,
Giannis Papadopoulos <> wrote:

>> In the statement "a *= expression" is expression assumed to be
>> parenthesized ? For example if I write "a *= b+c" is this the same
>> as "a = a * (b+c)" or "a = a * b+c" ?


>A compound assignment of the form E1 op = E2 differs from the simple
>assignment expression E1 = E1 op (E2) only in that the lvalue E1 is
>evaluated only once.


That's not very enlightening, since it doesn't answer the question
of whether E2 is b or b+c. In particular, the answer is different
for a *= b,c.

-- Richard
 
Reply With Quote
 
Rafael Almeida
Guest
Posts: n/a
 
      06-13-2006
On 13 Jun 2006 15:42:52 -0700
wrote:

> In the statement "a *= expression" is expression assumed to be
> parenthesized ? For example if I write "a *= b+c" is this the same
> as "a = a * (b+c)" or "a = a * b+c" ?
>

If you find this confusing why not simple use parenthesis or not use
the *= (and like) operator at all?
 
Reply With Quote
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      06-13-2006
Richard Tobin wrote:
> In article <e6nho5$ntu$>,
> Giannis Papadopoulos <> wrote:
>
>>> In the statement "a *= expression" is expression assumed to be
>>> parenthesized ? For example if I write "a *= b+c" is this the same
>>> as "a = a * (b+c)" or "a = a * b+c" ?

>
>> A compound assignment of the form E1 op = E2 differs from the simple
>> assignment expression E1 = E1 op (E2) only in that the lvalue E1 is
>> evaluated only once.

>
> That's not very enlightening, since it doesn't answer the question
> of whether E2 is b or b+c. In particular, the answer is different
> for a *= b,c.
>
> -- Richard


It says (E2) so why should it be further clarified?

--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
 
Reply With Quote
 
Chris McDonald
Guest
Posts: n/a
 
      06-13-2006
writes:


>Chris McDonald wrote:


>> writes:
>>
>> >In the statement "a *= expression" is expression assumed to be
>> >parenthesized ? For example if I write "a *= b+c" is this the same
>> >as "a = a * (b+c)" or "a = a * b+c" ?

>>
>> Write a small C program and try it.
>> The learning will provide benefits.
>>


>It wouldn't provide any more benefits than what I would get from
>a reply here. Plus experiments are a dangerous way of learning
>about C unless one knows beforehand that the behaviour being
>investigated
>is determined by the standard. If that's not the case then one will not
>know if what he observes is standard or implementation defined or
>undefined which just happened to work in a specific way under a
>specific
>compiler on a specific platform. For all I know , whether "expression"
>is
>parenthesized or not in "a *= expression" , is implementation defined
>and
>if one wants to achieve portability one should include parentheses in
>the
>code whenever the standard operator precedence does not give the
>desired
>result.



Sorry, politely, this is bordering on the absurd.
You do not trust anything you observe from experiments,
you do not trust anything you get from a reply here,
you do not trust anything you investigate to be conforming.

OK, it's your choice to be so suspicious and pedantic, but it's unclear
what you, or the OP, should ever trust.

Will you trust your own interpretation of the standard?
Will you trust anyone's interpretation of the standard?
And if you do trust your own or anyone else's interpretation of the standard,
how did you gain that trust?

If every commodity compiler such as MS-Studio or gcc chose to implement
the above assignment in an implementation defined fashion, then how is
anyone to rise to the level of even a basic user?

Given the nature of the OP's original question, it's clear to anyone
that a basic experiment, for varying values of a, b, and c, will reveal
far more insight than all the anal reflection in the world.

--
Chris.
 
Reply With Quote
 
spibou@gmail.com
Guest
Posts: n/a
 
      06-14-2006

Richard Tobin wrote:

> In article <e6nho5$ntu$>,
> Giannis Papadopoulos <> wrote:
>
> >> In the statement "a *= expression" is expression assumed to be
> >> parenthesized ? For example if I write "a *= b+c" is this the same
> >> as "a = a * (b+c)" or "a = a * b+c" ?

>
> >A compound assignment of the form E1 op = E2 differs from the simple
> >assignment expression E1 = E1 op (E2) only in that the lvalue E1 is
> >evaluated only once.

>
> That's not very enlightening, since it doesn't answer the question
> of whether E2 is b or b+c. In particular, the answer is different
> for a *= b,c.


I don't see any way to parse the expression "a *= b+c" so that E2 turns
out to be just b.

 
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
T::operator int () const ambiguous with T::operator Handle () const? Tim Clacy C++ 15 05-30-2005 02:14 AM
Member operators operator>>() and operator<<() Alex Vinokur C++ 3 03-20-2005 03:11 PM
operator*(Foo) and operator*(int) const: ISO C++ says that these are ambiguous: Alex Vinokur C++ 4 11-26-2004 11:46 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
Q: operator void* or operator bool? Jakob Bieling C++ 2 03-05-2004 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57