473,738 Members | 8,397 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 #1
56 4785
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.

--
Chris.
Jun 13 '06 #2

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

Jun 13 '06 #3
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.
--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
Jun 13 '06 #4
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.

-- Richard
Jun 13 '06 #5
In article <e6**********@v olcano1.grnet.g r>,
Giannis Papadopoulos <ip******@inf.u th.gr> 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
Jun 13 '06 #6
Richard Tobin wrote:
In article <e6**********@v olcano1.grnet.g r>,
Giannis Papadopoulos <ip******@inf.u th.gr> 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/
Jun 13 '06 #7
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.

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.
Jun 13 '06 #8
On 13 Jun 2006 15:42:52 -0700
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" ?

If you find this confusing why not simple use parenthesis or not use
the *= (and like) operator at all?
Jun 14 '06 #9

Richard Tobin wrote:
In article <e6**********@v olcano1.grnet.g r>,
Giannis Papadopoulos <ip******@inf.u th.gr> 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.

Jun 14 '06 #10

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

Similar topics

7
3692
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
4961
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
1376
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
5333
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
1536
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
1473
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
6079
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
4401
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
2954
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
8969
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
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
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
9208
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
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
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
4570
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
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.