# A=5;c=++a + ++a + ++a;What would be value of c after this statement and how does it come?If we have the codea=5;c=a++ + a++ + a++;What would be the value of c then?

#### srisujan

• Nov 25th, 2007

The value of c will be 24

because ++a being a pre incrementing operator it will increment the value of a before the expression being evaluvated..

thereby

c = ++5 + ++5 + ++5

c = 6          7         8       //this line specifies the value of a after incrementing and the last updated value is 8.

now evaluatin the expression

c = 8 + 8 + 8

c=24

#### Kamal

• Nov 25th, 2007

In the above case, a single variable is used multiple times, here 'a'.

Thus , the output is not guarantee the output. It can be anything, according to different increment and decrement behavior.

Such kind of statements should be avoided.

Regards,
Kamal

#### ashwini_nith Profile Answers by ashwini_nith Questions by ashwini_nith

• Nov 26th, 2007

Well executing the line
c=++a + ++a + ++a;

Gives the output as 22.
BUt I am not sure why it does so???

#### yehsi

• Nov 27th, 2007

if
a =5;
c=++a + ++a + ++a;

a=8;
c=6+7+8
= 22;

if
a =5;
c=a++ + a++ + a++;

a=8;
c=5+5+5
= 15;

#### kamal7725 Profile Answers by kamal7725 Questions by kamal7725

• Nov 29th, 2007

value will be 24, i.e
8 + 8 +8

why?

because compiler will add them after increment a's value. In this case compiler make two stack like
++a
++a               +
++a               +

and arithmetic operation will take place after evaluating final value of a, and that would be 8
now it time to made some mathematical, compiler take the operand i.e 8 and pop an operator '+' from the stack, so the expression would be
8 + 8 + 8 = 24;

#### busybee Profile Answers by busybee

• Dec 4th, 2007

case i  : (c=++a + ++a + ++a;)
c= 8+7+6
c=21

case ii : (c=a++ + a++  + a++;)
c=7+6+5
c=18

#### shetty.gprakash Profile Answers by shetty.gprakash

• Jan 22nd, 2008

For the first one answer c =  8 + 8 + 8 = 24 and a = 8

for the second one c = 5 + 5 + 5 = 15 and a = 8

#### sonalind Profile Answers by sonalind

• Jan 23rd, 2008

For first case value of the C will be 22.For 2nd case it is 15. Consider the first case:C = ++a + ++a + ++a; The above calculation will be carried out in the following sequence: 1) TempMem = (++a + ++a) (pre-increment operator has a preference so before addition,  value of the “a” is incremented twice and it became 7) so result of the eq1 is 14. 2) Again value of the ‘a’ is incremented and now it is 8. Now the remaining part of the     instruction will be carried out. 3) TempMem = TempMem + 8 = 22 4) C = TempMem Consider the second case:C = a++ + a++ + a++; post-increment operator has lower preference so the value of ‘a’ will be incremented thrice after addition, but for an addition the value will be unchanged i.e. 5.

#### rajeevdas2000 Profile Answers by rajeevdas2000

• Jan 25th, 2008

when a=5then c=++a + ++a + ++a=24(8+8+8)(last value of a will be 8)

#### milindd Profile Answers by milindd

• Jan 27th, 2008

Compile this code and see, answer is 21 (if a = 5)

Regards

#### smarter Profile Answers by smarter

• Jan 28th, 2008

The pre increment operator will be executed first then the assignment operator and then post increment operator executes.

In the following case:

a=5;
c=++a + ++a + ++a;

The value of c will be 24

because ++a being a pre incrementing operator it will increment the value of a and since there are 3 pre increment operations on a its value will be 8.

since it refers to same memory location (i.e. a and its value is 8) the expression will be evaluated as

c = 8 + 8 + 8 = 24.

Similarly for,

a=5;
c=a++ + a++ + a++;

here assignment operator executes first then post increment operator thus,

value at memory location a is 5 currently, it will be evaluated as

c = 5 + 5 + 5 = 15. then post increment operator will increment the value of a.

#### Amit_21 Profile Answers by Amit_21

• Jan 31st, 2008

a=5;
c=++a + ++a + ++a;

there are few steps here

(i) right most ++a will get incremented to 6
(2) and now 6 will be placed at memory location of a
(3) now for ++a , a become 7 ,and now 7 will be placed at memory location of a
(4) + is a binary operator it needs two value
(5) it will fetch the value of a which is 7
(6) now 7+7=14
(7) ++ has higher precedence
(8) now ++a + 14 is to be calculated
(9)a will be 7+1=8
(10)8+14=22

#### ashwini_nith Profile Answers by ashwini_nith Questions by ashwini_nith

• Jan 31st, 2008

@sonalind: if the compiler takes two additions at a time in the first case then should it not take two in the second case too, and the contrary also applies...

@milindd: Which compiler did you compile this code on when you got the answer as 21???

#### calvinss4 Profile Answers by calvinss4

• Feb 28th, 2008

MVC++ 2005 yields
1.) a == 8; c == 24;
2.) a == 8; c == 15;

gcc yields
1.) a == 8; c == 22;
2.) a == 8; c == 15;

Either one of the compilers is non-conforming, or the C standard doesn't dictate
exact requirements regarding the implementation of the increment operators vs.
the order of evaluation. As someone stated before, this type of code should be
avoided.

#### Amit_21 Profile Answers by Amit_21

• Feb 28th, 2008

c= a++ + a++ + a++;
5  +    5 +     5  =15
c=15;
now
a++    6
a++    7
a++    8
a=8

#### nishant1287 Profile Answers by nishant1287

• Mar 9th, 2008

becoz of higher precedence of preincrement  operater ,all the increment will be compiled first thn the value of c is comes out 24.

#### sshobhit Profile Answers by sshobhit

• May 14th, 2008

#### rashedbjit Profile Answers by rashedbjit

• Aug 26th, 2008

a=5;c=++a + ++a + ++a;

c=7+7+8

a=6;c=++a + ++a + ++a;
c=8+8+9

a=7;c=++a + ++a + ++a + ++a;
c=9+9+10+11

the calculation pattern is like above

#### vaynugopaal Profile Answers by vaynugopaal

• Oct 24th, 2008

c= ++a + ++a + ++a

c= 6+7+8=21

#### kwanzone Profile Answers by kwanzone

• Mar 13th, 2010

a is preincremented three times before addition.
a=5;c=++a + ++a + ++a;   //  a is 8; c is 24;

a is post-incremented three times after addition.
a=5;c=a++ + a++ + a++;   //  a is 8; c is 15;

>> MVC++ 2005 yields
>> 1.) a 8; c 24;
>> 2.) a 8; c 15;

>> gcc yields
>> 1.) a 8; c 22;
>> 2.) a 8; c 15;

Sorry to say the GCC is incorrect.

It has the result of this code.
a=5;
c=(++a + ++a);
c=c + ++a;

// a=8; c=22;

Yet, GCC doesn't yeild this code:
a=5;
c=(a++ + a++);
c=c + a++;
// a+8; c=17;

It is inconsistent.

#### kbjarnason Profile Answers by kbjarnason

• Jul 1st, 2010

The code invokes undefined behaviour, so the correct answer may as well be "a suffusion of yellow".

Specifically, the object a is evaulated and modified more than once between sequence points.  Since this is an undefined behaviour, any result - from 24 to 240000 to crashing the application to setting your nose on fire - is perfectly acceptable from the perspective of the languge.

If you want predictable results, you cannot use undefined behaviour.  If you use undefined behaviour, you cannot expect the results to be predictable.

Note: yes, on some implementations, the result may well be what you think "it should be" - one of the possible effects of undefined behaviour is appearing to do the "right" thing.  It's a particular pernicious effect, because it suggests the code is _required_ to do the "right" thing, even though it's not.

#### jbode Profile Answers by jbode Questions by jbode

• Dec 1st, 2010

The behavior of expressions like this is undefined.  Any result is possible (and as several people have pointed out, different platforms will yield different results, and as far as the language standard is concerned all of them are equally correct).  From the C language standard (draft n1256):

------------------------------
6.5  Expressions
...
2 Between the previous and the next sequence point an object shall have its stored value modified at most once by the evaluation of an expression 72).  Furthermore, the prior value shall be read only to determine the value to be stored 73).
...
72) A floating-point status flag is not an object and can be set more than once within an expression.

73) This paragraph renders undefined statement expressions such as

i = i++ + 1;
a[i++] = i;

while allowing

i = i + 1;
a[i] = i;

----------------------------------------------

You cannot rely on the side effect being applied immediately after the expression is evaluated; the only requirement is that the side effect be applied before the next sequence point.  Assume a well-defined expression like

c = ++a + ++b;

the following order of operations is possible:

tmp1 <- a + 1
tmp2 <- b + 1
c <- tmp1 + tmp2
a <- a + 1
b <- b + 1

In this case, a and b are not updated until after the assignment to c.

#### Shravan Konda Profile Answers by Shravan Konda

• May 16th, 2011

c=28

a=8

#### Abani Sahoo Profile Answers by Abani Sahoo

• Jul 5th, 2011

a=5;
c=++a + ++a + ++a;
Actually the answer will varry from  compiler to compiler

->>>>    If you are running with turbo C then the output will be 24
c=++a + ++a + ++a;
6            7        8
c=8+8+8
c=24

->>>>>> If you are running with GCC(LINUX OS) then the output will be 22
c=++a + ++a + ++a;
6          7
7         7        8
c=7+7+8
c=22

Answer to c=a++ + a++ + a++ is same in both Turbo C as well as GCC i.e 15
c=a++ + a++ + a++
5         5       5
c=5+5+5
c=15

to know better about increament and decreament operation plz ask me the questions if you have any.........

#### sreesha

• Jul 30th, 2013

18 15

#### Pratibha Guntreddy

• Aug 21st, 2013

for the pre increment c=25, i.e., c=6+7+8=25
for post increment c=9, i.e., c=4+3+2=9

#### pankaj

• Aug 22nd, 2013

in first c=8+7+6=21
and second c=7+6+5=18

• Aug 27th, 2013

in the first case:
a=5;
c= ++a + ++a + ++a;
now in first pre increment the value of a will be 6 and after 1st pre-increment the value in secont pre-increment statement will be 7, so that the value in the first incremented will be updataed to 7 but when access reaches to 3rd increment statement the value of a will be 8 so that value will be updated to the neighbouring varialbe i.e,2nd pre-increment statement..............and the updated value in the first pre-increment i.e,7 will be lost and 6 will be stored
hence, we my write in steps:
1st- c=6
2nd- c=6+7 -->>>> c=7+7(updation)
3rd - c = 7+7+8 -->> c = 7+8+8(1st updation) -->> c = 6+8+8(final updation) = 22

you may check it with further increment statements like:
a=5
c= ++a + ++a + ++a + ++a + ++a;
result will be:6+7+8+10+10 = 41

#### Jitendra Kumar Profile Answers by Jitendra Kumar Questions by Jitendra Kumar

• Sep 8th, 2013

In the first case it gives answer 22 because it form a tree like structure and start from bottom i.e
++a and ++a in first ++a=6 and in the 2nd time ++a=7 and again aad take max[6,7] in the case of re-increment
for both ++a and it give 7 and add gives 14 now a=7 and again ++a then a becomes 8 and add all gives 22

#### shweta gupta Profile Answers by shweta gupta

• Jun 25th, 2014

It is completely dependent on the compiler to evaluate. we can see different behavior on different compilers.
Example if u use turbo c the find the o/p of c=++a + ++a + ++a; is 24 and for c=a++ + a++ + a++;is 15.
if you use gcc then o/p is based on the if we use pre increment then it place for c=++a + ++a + ++a;
c=6+6+7=22 and for c=a++ + a++ + a++; is c=5+6+7=18.

#### atul kumar

• Aug 20th, 2014

If you compile code by turboc compiler then you will get 21 while in gcc you will get 22 for first statement

#### Gurpreet Thind

• Nov 24th, 2014

Ans is 21
First the pre incrementor will work on all as and then it will be added.
so first ++a will make it 6
second will make it 7
and third will make it 8
adding all 3 is done after words so ans is 21

in second case
ans is 18
post increment will assign the value and then increment so a=5 and first a++ will keep it 5 and then increment to 6
second will make it 6 and then increment to 7
third will make it 7 and then increment to 8

#### elayaraja.v

• Dec 16th, 2014

a=5
c=a++ + a++ + a++;
c=7,a=8;

#### elayaraja.v

• Dec 16th, 2014

c=18,a=8

#### elayaraja.v

• Dec 16th, 2014

case ii is correct but not case i

#### anshul maheshwari

• Dec 27th, 2014

Answer is 18   because question uses post fix notation...
So 5+6+7 =18

#### ranjan

• Jan 7th, 2015

24 because every time a is inc. by 1 ie. 6,7,8 now present value of a is 8 that is why 8+8+8=24

#### rajasekhar

• Mar 12th, 2015

19

```Codea++ + ++a + a++
a = 5
a++ = 5
++a = 7
++a = 7
so 5 + 7+ 7 = 19```

#### Narasimha Kulkarni

• Mar 14th, 2015

The code given is illegal in C/ CPP/ Perl and many other languages, and the behavior of compiler is not defined.
C language has points in code called Consistency points. Consider following two statements,
a =b; and a=b++;
The effect of first statement is to initialize value of b to a, and that is it. However the second statement has a side-effect of incrementing value of b. Consistency point in C is a point in code where the side-effects have been neutralized. In C, a comma, semi-colon form consistency points. In short, two different compilers may behave differently between two consistency points, but behave same at a consistency point. As laid down by rules of ISO, the value of a variable can be altered only once between a consistency point. When the code tries to alter a variable twice between a CP, compiler can do anything ( It may attack Gotham if possible, but most compilers wont) and it is still a valid compiler. The above statement a++ + a++ + ++a tries to alter a thrice in a CP, and the output cannot be predicted.

#### brajesh soni

• Mar 26th, 2015

21 = (6 + 7 + 7(+))( assosiativity rules)

#### Munish

• Jun 2nd, 2015

```Codeif a =5;
c=++a + ++a + ++a;
c = 6+7+8 = 21;

if a =5;
c=a++ + a++ + a++;
c=5+6+7 = 18;```

#### Munish

• Jun 2nd, 2015

```Codeif a =5;
int c = ++a + ++a + ++a;
|          |         |
6           7        8     ; value of a becomes  8;
c = 6+7+8 = 21;

if a =5;
int c = a++ + a++ + a++;
|          |         |
5           6       7     ; value of a becomes 7;
c = 5+6+7 = 18;```

#### Kritika kajale

• Jun 12th, 2015

In case of preincrement , the value is increased first and then other operations. So, in case of
c=++a(6) + +a(7) + ++a(8) = 21
In case of post-increment, first processing and then increment operation is performed. So, in later case:
c= a++(5) + a++(6) +a++(7)=18

#### Anonymous

• Aug 3rd, 2015

6+7+8 = 21...

#### aftab

• Nov 17th, 2015

Totally compiler dependent !

#### Nitin Garg

• Dec 12th, 2015

It is very easy
i=5;
++i + ++i + ++i;
++ is higher precedence so that
++i + ++i + ++i

In this equation first i increment by 1
i=6
then again increment by 1
i=7;
then add first two is (++i + ++i) so addition is 14
then again increment by 1
i=8

#### Alejandro Visiedo

• Mar 5th, 2016

It is the same as:
a = 5;
c = (a=a+1) + (a=a+1) + (a=a+1);
and the second block would be the same as:
c = a + (a=a+1) + (a=a+1); a=a+1;
So that, the result will be:
* a=8; c=21, in the first case.
* a=8; c=18, in the second case.

#### harika

• Aug 10th, 2017

c = ++a + ++a + ++a; ans is 22 but how 6+7+8 = 21 how it became 22 !!

#### jbode Profile Answers by jbode Questions by jbode

• Aug 22nd, 2017

@harika: Because the behavior of ++a + ++a + ++a is *undefined*. There is *no* correct answer. Operator precedence doesnt control order of evaluation, and order of evaluation is *unspecified* except in a few cases. Theres no guarantee that the expressions are evaluated left to right, and theres no guarantee that the side effect of the ++ operator is applied immediately. The result *will* vary based on hardware, compiler, optimization settings, and surrounding code, any *any* result is considered incorrect, even if its the value you expect.

#### Dnyaneshwar

• Sep 6th, 2017

C = ++a + ++a + ++a;
Ans is 24 As consider to Turbo C 3.0
Because above expression is Register evaluation, thats why last updated value of a is used for all a in expression and last updated value is 8 ...(c=6+7+8 because of pre-increment.)
C=8+8+8=24

#### Anil kumar

• Mar 25th, 2019

it is study of prefix behaviour.
i got it
if a=5,b=0;
it will like
(1+6)+6+7×3;
when, has 1st var prefix
++a; add 1 extra after full line calculation if 1st var is post fix and next even pre, no need to add 1 extra

#### Pujitha

• Jun 27th, 2019

It is very easy
A=5;
C=++a + ++a + ++a;
C=8+8+8;
C=24.
A=8.
A=5;
C=a++ + a++ + a++;
C=5+5+5;
C=15.
A=8

#### Rakesh

• Aug 18th, 2019

For c++14 it is 22, for A=5

```Code#include <iostream>
using namespace std;

int main() {
int a=5,b;
b=++a + ++a + ++a;
cout<<b;
return 0;
}```

#### semayawi

• Nov 20th, 2019

#include < iostream >
using namespace std;
int main()
{
int a=5,c;
c=++a + ++a + ++a;
cout < < c;
//first excretion a=5 and ++a means a+1= 5+1=6.
// second excretion a=6 and ++a means a+1 = 6+1=7.
// the last excretion a=7 and ++a means a+1 = 7+1= 8
// =>there fore the final result will be 8+8+8 = 24
}  