473,881 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about the *= (and similar) operator

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" ?

Jun 13 '06
56 4842

Keith Thompson wrote:
sp****@gmail.co m writes:
Keith Thompson wrote:
sp****@gmail.co m 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" ?

All assignment operators, including "*=" and ordinary "=", have the
same precedence.

Just as "a = b + c" means "a = (b + c)", "a *= b + c" means "a *= (b + c)".

Your C textbook should have an operator precedence table or equivalent.
Well , I saw my question as one about semantics rather than operator
precedence.


No, it was a question about operator precedence (actually about how
expressions are parsed, since the standard doesn't talk about
precedence as such). The semantics of the expression follow from the
way it's parsed (and from the semantics of the operations themselves).


Perhaps that's how you read it but precedence was not what I was
confused
about. See below.
sp****@gmail.co m wrote:
Richard Tobin wrote:
In article <e6**********@v olcano1.grnet.g r>,
Giannis Papadopoulos <ip******@inf.u th.gr> wrote:
>> 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.

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


Your reasoning would lead to the (wrong) conclusion that a *= b,c is
equivalent to a = a * (b,c).

Yes, you put parentheses around E2, but what *is* E2? Is it b
or b op c?


In the general case , E2 would be the right operand of the *= operator.
So in the expression "a *= b,c" we use operator precedence to decide
that the right operand of the *= operator is b and combining that
with what Giannis said we conclude that "a *= b" is the same as
"a = a * (b)".
The standard answers the question by means of the sequence of
productions in section 6.5. a *= b + c is parsed as a *= (b + c)
because the the right operand of an assignment operator can be an
additive expression, which b + c is; whereas the left operand of an
additive expression must be an additive expression, which a *= b
isn't.


Actually this is something which does not answer my question. I *knew*
when I made the opening post that "a *= b + c" is parsed as "a *= (b +
c)"
ie that the right hand operand of *= is "b+c" but I wasn't completely
certain how the right hand operand is to be used to alter the value of
the
left hand operand. See also the comment below.
Richard Tobin wrote:
In article <11************ **********@g10g 2000cwb.googleg roups.com>,
<sp****@gmail.c om> wrote:
In the statement "a *= expression" is expression assumed to be
parenthesize d ? 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.


Yes , after the explanations from various people I see now that the
alternative I had in mind was rather implausible. But I'm glad I
verified it.


Jun 14 '06 #21

Rafael Almeida wrote:
On 13 Jun 2006 17:20:13 -0700
sp****@gmail.co m wrote:

Keith Thompson wrote:
All assignment operators, including "*=" and ordinary "=", have the
same precedence.

Just as "a = b + c" means "a = (b + c)", "a *= b + c" means "a *= (b + c)".

Your C textbook should have an operator precedence table or equivalent.


Well , I saw my question as one about semantics rather than operator
precedence.

Are you saying keith didn't answer your question?


By the time I read Ketih's post I had read others which had answered my
question. I'm not sure if Keith's post would have done it on its own. I
was
simply saying that he didn't understand what I was confused about.

Jun 14 '06 #22

Giannis Papadopoulos wrote:
sp****@gmail.co m 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.


Although that answers my question I'm confused about the "only once"
part. How many times is E1 evaluated in "E1 = E1 op (E2)" ? Or to be
more specific how many times is "a" evaluated in "a = a * (b+c)" ?

Jun 14 '06 #23
sp****@gmail.co m schrieb:
Giannis Papadopoulos wrote:
sp****@gmail.co m 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.


Although that answers my question I'm confused about the "only once"
part. How many times is E1 evaluated in "E1 = E1 op (E2)" ? Or to be
more specific how many times is "a" evaluated in "a = a * (b+c)" ?


Well, a appears two times, so its evaluated two times. Simple?

Thomas
Jun 14 '06 #24

Thomas J. Gritzan wrote:
sp****@gmail.co m schrieb:
Giannis Papadopoulos wrote:
sp****@gmail.co m 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.


Although that answers my question I'm confused about the "only once"
part. How many times is E1 evaluated in "E1 = E1 op (E2)" ? Or to be
more specific how many times is "a" evaluated in "a = a * (b+c)" ?


Well, a appears two times, so its evaluated two times. Simple?


Nope ! Perhaps I have the wrong idea about what "evaluated" means.
Could you explain that ? Consider something simpler first: "a=1". What
is the evaluation of "a" in this case ?

Jun 14 '06 #25
sp****@gmail.co m writes:
Giannis Papadopoulos wrote:
sp****@gmail.co m 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.


Although that answers my question I'm confused about the "only once"
part. How many times is E1 evaluated in "E1 = E1 op (E2)" ? Or to be
more specific how many times is "a" evaluated in "a = a * (b+c)" ?


In "a = a * (b + c)", a is evaluated twice -- but in this case it
doesn't matter (unless a happens to be volatile).

This becomes more relevant if the expression has side effects.

#include <stdio.h>

int func(char *message)
{
printf("In func(\"%s\")\n" , message);
return 0;
}

int main(void)
{
int arr[1] = { 0 };

arr[func("Not using *=")] = arr[func("Not using *=")] * 42;
arr[func("Using *=")] *= 42;

return 0;
}

This prints:

In func("Not using *=")
In func("Not using *=")
In func("Using *=")

--
Keith Thompson (The_Other_Keit h) 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.
Jun 14 '06 #26
On 2006-06-14, sp****@gmail.co m <sp****@gmail.c om> wrote:

Chris McDonald wrote:
sp****@gmail.co m writes:

>Chris McDonald wrote:

>> sp****@gmail.co m writes:
>>
>> >In the statement "a *= expression" is expression assumed to be
>> >parenthesize d ? 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
>parenthesize d 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.


I don't see how the above statements follow from what I said.
OK, it's your choice to be so suspicious and pedantic, but it's unclear
what you, or the OP, should ever trust.


I don't feel that the words pedantic and suspicious apply at all to
what I said.
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?


These qustions are outside the topic of the thread.
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?


I suggested the possibility that the choice between *only 2*
interpretations
could be up to individual implementations . Even without such a
restriction
I don't see how it would have anything to do with one's ability to rise
to the
level of basic user.

This is a basic question. If you can't understand it, (or if implementation
differences make it impossible to understand it), you can't become 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.


I can't imagine what insight you think it would offer but I'm pretty
sure it wouldn't answer my question. In any case why don't you run
the experiment and tell us what insight you gained ?


It will answer your question, and Chris won't run the experiment because
he knows C, and already knows the answer. He's merely encouraging you to
figure it out yourself, and hence learn to teach yourself.

If you give a man a fish, he'll eat for a day...

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 14 '06 #27
Keith Thompson wrote:
sp****@gmail.co m writes:
Giannis Papadopoulos wrote:
sp****@gmail.co m 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.
Although that answers my question I'm confused about the "only once"
part. How many times is E1 evaluated in "E1 = E1 op (E2)" ? Or to be
more specific how many times is "a" evaluated in "a = a * (b+c)" ?

In "a = a * (b + c)", a is evaluated twice -- but in this case it
doesn't matter (unless a happens to be volatile).


I take it that if it's volatile then its value may change between
evaluations.
This becomes more relevant if the expression has side effects.

#include <stdio.h>

int func(char *message)
{
printf("In func(\"%s\")\n" , message);
return 0;
}

int main(void)
{
int arr[1] = { 0 };

arr[func("Not using *=")] = arr[func("Not using *=")] * 42;
arr[func("Using *=")] *= 42;

return 0;
}

This prints:

In func("Not using *=")
In func("Not using *=")
In func("Using *=")
That was very enlightening. Thank you.

Andrew Poelstra wrote:
On 2006-06-14, sp****@gmail.co m <sp****@gmail.c om> wrote:

Chris McDonald wrote:
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?

I , spibou the great , wrote:

I suggested the possibility that the choice between *only 2*
interpretations
could be up to individual implementations . Even without such a
restriction
I don't see how it would have anything to do with one's ability to rise
to the
level of basic user.

This is a basic question. If you can't understand it, (or if implementation
differences make it impossible to understand it), you can't become a basic
user.


I understand the question and I understand straightforward answers to
the question. I have written a fair amount of above basic programmes
in C therefore it has been possible for me to become more than a basic
user of the language without knowing the answer to the question.
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.


I can't imagine what insight you think it would offer but I'm pretty
sure it wouldn't answer my question. In any case why don't you run
the experiment and tell us what insight you gained ?


It will answer your question, and Chris won't run the experiment because
he knows C, and already knows the answer. He's merely encouraging you to
figure it out yourself, and hence learn to teach yourself.


Since you're claiming that it would answer my question can you explain
what
series of arguments starting from the result of the experiment and I
don't know
which other premises would lead me to conclude that the behaviour I
observed
was standard as opposed to implementation defined ?
Otherwise , Chris may know the answer to the question but he claimed
that the
experiment would offer me "far more insight". I don't know what this
substantial
amount of insight is supposed to be. I'm not even sure whether an
answer to my
question is supposed to be part of this insight.
The replies in the thread have offered me insight. The reply by Keith
above for
example or the quote form the standard or the comparison between macros
and
operators given in a different post (not quoted here). I wouldn't have
gotten any
of that from an experiment even if it would have answered my question
and I
don't believe it would have.
If you give a man a fish, he'll eat for a day...


So you're saying that writing and running a small C programme would
teach me
about fishing. But I'm a city person you see , I don't fish. If I want
fish I just go
to the supermarket.

Spiros Bousbouras

Jun 14 '06 #28
sp****@gmail.co m writes:
Keith Thompson wrote:

[...]
In "a = a * (b + c)", a is evaluated twice -- but in this case it
doesn't matter (unless a happens to be volatile).


Yes, that too. Volatility goes both ways: external mechanisms can
change a volatile variable's behind the program's back, and any access
(either a read or a write) of a volatile variable is considered a side
effect, so it must actually happen. Given:

{
volatile int x;
int y;
x = 10;
x = 20;
y = x;
printf("y = %d\n", y);
}

the generated code must actually store the values 10 and 20 to x, and
then read x to store its value in y. Without the "volatile", the
compiler could legally entire chunk of code to:

puts("y = 20");

(In practice, there's no good reason to apply "volatile" to an auto
object; more commonly it might be applied to an object at a specified
address, such as a hardware register of some kind.)

--
Keith Thompson (The_Other_Keit h) 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.
Jun 14 '06 #29
On Wed, 14 Jun 2006 08:21:54 GMT, Keith Thompson <ks***@mib.or g>
wrote:
sp****@gmail.c om writes:
Keith Thompson wrote:

[...]
In "a = a * (b + c)", a is evaluated twice -- but in this case it
doesn't matter (unless a happens to be volatile).


Yes, that too. Volatility goes both ways: external mechanisms can
change a volatile variable's behind the program's back, and any access
(either a read or a write) of a volatile variable is considered a side
effect, so it must actually happen. Given:

{
volatile int x;
int y;
x = 10;
x = 20;
y = x;
printf("y = %d\n", y);
}

the generated code must actually store the values 10 and 20 to x, and
then read x to store its value in y. Without the "volatile", the
compiler could legally entire chunk of code to:

puts("y = 20");

(In practice, there's no good reason to apply "volatile" to an auto
object; more commonly it might be applied to an object at a specified
address, such as a hardware register of some kind.)


I can't think of any reason why a volatile auto object that is not a
pointer and is not pointed to by a suitably typed pointer could ever
be useful. Sure, C may allow that, but what use could it ever be?

--
jay
Jun 14 '06 #30

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

Similar topics

7
3695
by: Jessica | last post by:
Hi, I have a design question. I am making a time series analysis tool. Since I already use STL vector to represent time series, is there a need to implement a class for the time series object? Right now I just use typedef vector<double>TS; Is that enough? I feel that I am not taking advantage of the C++
3
4977
by: Stephan Kurpjuweit | last post by:
Hi, could you please help me with the following example? struct Base { virtual Base& operator = (const Base &k) {} };
8
1383
by: Derek Long | last post by:
I believe that the following is valid c++: #include <iostream> template <typename CMP> class Comparator { bool operator()(int x,int y) { return CMP(x,y);
105
5387
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does not work. It only works on lists and strings, for no obvious reason. Why not on all sequence types? Or, another example, the index() method has start and end parameters for lists and strings. The count() method also has start and end parameters...
18
1553
by: Xiaoshen Li | last post by:
Dear All, I have a question regarding to linking separate files for compilation. For example, I have two file in the same directory: file1.cpp & file2.cpp. ==> file1.cpp <== int iNumber= 999; ==> file2.cpp <== #include <iostream>
10
1489
by: Jeroen | last post by:
Hi guys, Just another question. Suppose I have 2 classes (incomplete code): class A { A(const B& b); A& operator = (const A& a); }; class B {
16
6095
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last element in the container? Or is there a cleaner way of getting an iterator to the last element?
8
4407
by: JackC | last post by:
Hi, I am trying to get posix threads working from within an object, heres some code: int NConnection::TheadControl() { int thread_id; pthread_t new_connection; pthread_create(&new_connection, NULL, PriC, (void *)thread_id);
3
2966
by: Michel Esber | last post by:
Hi all, Db2 v8 FP15 LUW . create table T (ID varchar (24), ABC timestamp) There is an index for (ID, ABC), allowing reverse Scans. My application needs to determine MIN and MAX(ABC) for a given ID. We are currently using a simple statement:
0
9928
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10718
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10816
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10402
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7111
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5781
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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 we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.