473,382 Members | 1,255 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,382 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 1281
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.