473,406 Members | 2,769 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,406 software developers and data experts.

value of ++a+++a when a=8,how comuter calculate this?

int a=8,b;
b=++a+++a;

please tell me how the value of b becomes 20 ?
Jun 24 '14 #1
5 1282
weaknessforcats
9,208 Expert Mod 8TB
Who knows.

The rules allow that a variable can be changed only once in a statement.

Because of that rule, the compiler can evaluate the statement in any order. Right to left, or left to right and get the correct answer. Violate the rule, as this code does, and you get garbage (er, indeterminate results).
Jun 24 '14 #2
horace1
1,510 Expert 1GB
++ has a higher precedence than + therefor the expressions is
Expand|Select|Wrap|Line Numbers
  1. b=++a + ++a;
a is incremented twice (a then = 10) then a+a calculated and assigned to b which gives b=20

what about?
Expand|Select|Wrap|Line Numbers
  1. b=++a + ++a + ++a;
Jun 24 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
Did you read my post #2?
Jun 24 '14 #4
horace1
1,510 Expert 1GB
The critical thing to remember is do not use a variable more than once in an expression if one (or more) of the references has one of ++ or -- operators attached to it. The standard does not specify the order in which the operands of an operator are evaluated and there is no guarantee when an affected variable will change its value.
in the case of
Expand|Select|Wrap|Line Numbers
  1. b=++a + ++a;
it is not critical, the ++a operands will be evaluated before the + operator so the result will be a=10 and b=20

consider
Expand|Select|Wrap|Line Numbers
  1. b=++a + ++a + ++a;
the + operator associates from left to right so it evaluated
Expand|Select|Wrap|Line Numbers
  1. b=(++a + ++a) + ++a;
however, when are the ++a operands evaluated?
1. the ++a operands of (++a + ++a) evaluated and the result added then the final ++a evaluated and added? the result is a=11 and b=31
2. all the ++a operands evaluated and the then the + operations executed giving a=11 and b= 33
Jul 5 '14 #6

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

Similar topics

1
by: Ajay Brar | last post by:
hi! i get a value error when parsing an xml file. This is because it can't find the DTD - ValueError: unknown url type: ../um_xml/um.dtd From what i have discovered in the archives, this...
5
by: n_o_s_p_a__m | last post by:
Can't compile. Does this mean that all functions that throw exceptions must be of return type void? examples: // won't compile: "not all code paths return a value" public override int Run() {...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
3
by: Steve Yerkes | last post by:
There seems to be way too much confusion over how to set focus on the a field using a field validator. I looked all over the web and found people trying to do this, but not getting anywhere. There...
0
by: sandydon | last post by:
when am trying this in xsl page <xsl:variable name="myArray"><item>item1</item><item>item2</item></xsl:variable> <xsl:value-of select="$myArray/item"/> i got the following error HTTP...
0
by: DR | last post by:
I set my database to trustworthy and deployed this clr stored proc as unsafe ok but when i run this it returns 1 each time. as if ival keeps getting set back to 0 each time i call this clr stored...
7
by: cyberlei | last post by:
Hello Guys, Just wondering how I can make the substr value changes when submit into mysql. something like: someone typed A123456 on a blank field and submit into mysql. the Value will only...
2
by: ahmed222too | last post by:
when i use this code to copy a text file and paste it in a specific path: Dim fso As New FileSystemObject fso.CopyFile "C:\Files\file.txt", "C:\Files1\file_copy.txt" this error appear ...
0
by: Elias Mitchell | last post by:
hello i have a big problem, i'm useing microsoft access to create a database and there is a field called netbook value that is driven by this code, =-(DateDiff("m",,DateValue(Now()))**/1200). Now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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,...
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
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,...
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...
0
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...
0
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...

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.