473,440 Members | 1,771 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,440 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 1284
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...
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
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
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,...
0
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...
0
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...

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.