473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

operator precedence

424 256MB
For int a=5, why does a++ * ++a = 36 because * acts left-to-right and ++a pre-increments a so we should have 5x7=35. My books all say that ++ has precedence over *.
May 15 '08 #1
14 1798
Sick0Fant
121 100+
For int a=5, why does a++ * ++a = 36 because * acts left-to-right and ++a pre-increments a so we should have 5x7=35. My books all say that ++ has precedence over *.
I'm not entirely sure, but a++ will only increment after the end of the statement, while ++a will increment a before the statement.

So, since we have ++a in the statement, a = 6

so a++ * ++a = 36

and after that statement, a = 7.
May 15 '08 #2
oler1s
671 Expert 512MB
Read: C-FAQ. It's an undefined expression.

Sickofant, your explanation is incorrect. The postincrement does not mean after the statement.
May 16 '08 #3
JosAH
11,448 Expert 8TB
Altering a modifiable left-hand value in an expression more than once (*) causes
'undefined behaviour' as defined by the Standard, so discussing what the
outcome of the expression above will or should be is useless. Evaluating
that thing can even make daemons fly out of your nose: it is undefined.

kind regards,

Jos

(*) the actual text defines a 'sequence point'; an lhv may only be altered at most
once before that sequence point is reached.
May 16 '08 #4
arnaudk
424 256MB
Jos, just out of curiosity, is that also true for Java?
May 16 '08 #5
JosAH
11,448 Expert 8TB
Jos, just out of curiosity, is that also true for Java?
No, Java defines the evaluation as strictly from left to right and 'eager', i.e. an
assignment is performed as soon as possible (that includes the ++ and --
operators). So in Java the following results in b=35:

Expand|Select|Wrap|Line Numbers
  1. int a= 5;
  2. int b= a++*++a;
  3.  
I find it a mixed blessing because its evaluation scheme disallows for a bit of
optimization in certain expressions.

In C (and C++) daemons may fly out of your nose of course ;-)

kind regards,

Jos
May 17 '08 #6
arnaudk
424 256MB
OK. Thanks for the replies!
May 18 '08 #7
questionit
553 512MB
No, Java defines the evaluation as strictly from left to right and 'eager', i.e. an
assignment is performed as soon as possible (that includes the ++ and --
operators). So in Java the following results in b=35:

Expand|Select|Wrap|Line Numbers
  1. int a= 5;
  2. int b= a++*++a;
  3.  
I find it a mixed blessing because its evaluation scheme disallows for a bit of
optimization in certain expressions.

In C (and C++) daemons may fly out of your nose of course ;-)

kind regards,

Jos
Jos,

Just wondering after reading your above piece of code:

What would remain the advantage of post-increment operator (++) in this case when it is behaving same as pre-increment operator?

Expand|Select|Wrap|Line Numbers
  1. int a= 5;
  2. int b= a++*++a;
  3.  
It would be expected of a++ to increment after the statement and for ++a to increment during the statement to see their different. If in above example a++ is becoming 6 and then ++a is becoming 7, means both the operators (pre & post increment) are doing the same thing, where is the main different between them then?

Btw, did i not get what you mean by your statement" an assignment is performed as soon as possible" ... do you mean result of a++ is first assigned to the variable b and then this result is further evaluated with rest of the expression: * ++a ?


Regards
Qi
May 18 '08 #8
JosAH
11,448 Expert 8TB
Jos,

Just wondering after reading your above piece of code:

What would remain the advantage of post-increment operator (++) in this case when it is behaving same as pre-increment operator?

Expand|Select|Wrap|Line Numbers
  1. int a= 5;
  2. int b= a++*++a;
  3.  
It would be expected of a++ to increment after the statement and for ++a to increment during the statement to see their different. If in above example a++ is becoming 6 and then ++a is becoming 7, means both the operators (pre & post increment) are doing the same thing, where is the main different between them then?
No, the semantics of the a++ expression are: the value is the old value of a and
the variable a is incremented. The semantics of ++a are: increment a and the
value of the expression is the new value of a.

It all has nothing to do with "after the expresssion": things happen in a stricty
defined way in Java, so a++*++a is evaluated as:

1) take old value of a (5) and increment a (a is now 6)
2) the left hand operand of the * operator is 5
3 increment a (a is now 7) and get its value
4 the right hand operand of the * operator is 7
5 multiply the left and right hand operands values (5 and 7)
6) the result is 5*7 == 35

In C and C++ steps 1) and 3) are not allowed together in one expression because
the both alter the value of a modifiable left hand value (variable a).

kind regards,

Jos
May 18 '08 #9
excuse me, but i would like to clarify something on the topic..

isnt it that "variableName++" implies that "variableName" is to be incremented by one, after the statement/statements are done? so in this case, i guess what happens is:

a = 5;

lets say "a++ * ++a = x"

as far as i know:

a++ = 5; since the statement has not finished executing yet; and
++a = 6; since "a" is already incremented by one even before the statement executes; so

= (a++) * (++a)
= 5 * 6
= 30

i dont know if that's right or wrong..just wanted to clarify if this one is right or wrong..have a nice day!
May 18 '08 #10
JosAH
11,448 Expert 8TB
i dont know if that's right or wrong..just wanted to clarify if this one is right or wrong..have a nice day!
Sorry, it is dead wrong; have a nice day too.

kind regards,

Jos
May 18 '08 #11
arnaudk
424 256MB
My understanding of the post increment is that the value is incremented as soon as it is read: Since * acts from left to right, it reads a=5. This is the point where a is incremented because it has just been read. In java, things proceed like Jos described and in C/C++ you get an answer that defies all logic (for my compiler, at least, since strictly speaking any further toying with a apparently leads to undefined behaviour according to the standard).
May 18 '08 #12
arnaudk
424 256MB
Here is a catalog of results made with VC++2008 (a is reset to 5 before each line):
Expand|Select|Wrap|Line Numbers
  1. a++ * a   = 25, now a = 6.
  2. a   * a++ = 25, now a = 6.
  3. ++a * a   = 36, now a = 6.
  4. a   * ++a = 36, now a = 6.
  5. a++ * ++a = 36, now a = 7.
  6. a++ * a++ = 25, now a = 7.
  7. ++a * ++a = 49, now a = 7.
  8. ++a * a++ = 36, now a = 7.
  9.  
It appears that increments are made either entirely before or entirely after the multiplication operation so my previous statement wasn't correct, see line 4, for example. That it defies all logic is true, however.
[update: I tried it with GCC 3.4.6 on Redhat and got identical results.]
May 18 '08 #13
oler1s
671 Expert 512MB
My understanding of the post increment is that the value is incremented as soon as it is read:
Is this an understanding you got from reading the standard (you can get the circa 98 drafts online) or something you imagine is happening? Because there is a precise definition of how post increment works, and that definition is not what you described.

Here's the thing about undefined operations. Trying to put logic to them doesn't work, because you can't define undefined expressions. Maybe you can figure out how a certain compiler tends to interpret such undefined operations. Frankly, I fail to see the value gained.
May 18 '08 #14
arnaudk
424 256MB
...that definition is not what you described.
Sorry, I thought that was clear from my previous post.
...you can't define undefined expressions.
You don't seriously think I was trying to do this? It was merely unclear to me precisely what was and wasn't defined. Anyway, I found the relevant portion in the standard so, for completeness, here it is:

ISO/IEC 14882:1998, section 5.0, paragraph 4: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored."

Expressions like a*++a, whilst modifying the value of a only once, access it to determine the (incremented) value to be stored and again to determine the outcome of the multiplication, counter to the wording of the standard.
May 18 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
20
by: Vivek N | last post by:
Hi Folks, This question may well have been asked and answered before. But, sorry that I couldn't find one from the archives. I typed up this program and compiled it with gcc 3.3.2 main() { int...
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
17
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
56
by: spibou | last post by:
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" ?
9
by: marko | last post by:
/* code start */ int a = 0; /* expected evaluation and excution order with precedence in mind /* False(3) , True(1), False(2) */ if ( (a=1) == 0 || 0 != 1 && (a =2) == 1) putchar('T');...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
8
by: subramanian100in | last post by:
Consider int i = 10; Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ? I thought both these expressions yield only values. I am unable to understand the difference
6
by: newbie | last post by:
I have a script given to me by a co-worker to convert into VB; I can get the same results but I cannot fully understand the logic here... can someone please help me write this in a more...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.