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?

Questions by ashwini_nith   answers by ashwini_nith

Showing Answers 1 - 54 of 54 Answers


  • 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..


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



  • 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.


  Was this answer useful?  Yes


  • Nov 27th, 2007

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

  = 22;

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

  = 15;

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


because compiler will add them after increment a's value. In this case compiler make two stack like
++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;

  Was this answer useful?  Yes


  • Dec 4th, 2007

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

case ii : (c=a++ + a++  + a++;)

  Was this answer useful?  Yes


  • 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.


  • 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:

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,

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.

  Was this answer useful?  Yes


  • Jan 31st, 2008

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

@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???

  Was this answer useful?  Yes


  • 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


  Was this answer useful?  Yes


  • Feb 28th, 2008

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

  Was this answer useful?  Yes


  • Aug 26th, 2008

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


a=6;c=++a + ++a + ++a;

a=7;c=++a + ++a + ++a + ++a;

the calculation pattern is like above

  Was this answer useful?  Yes


  • 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.
c=(++a + ++a);
c=c + ++a;

// a=8; c=22;

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

It is inconsistent.

  Was this answer useful?  Yes


  • 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.

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. 

  Was this answer useful?  Yes

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
finally 8 will be assign in all a's

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

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

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

  Was this answer useful?  Yes


  • Jul 30th, 2013

18 15

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes


  • Aug 22nd, 2013

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

  Was this answer useful?  Yes

narender yadav

  • Aug 27th, 2013

in the first case:
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:
c= ++a + ++a + ++a + ++a + ++a;
result will be:6+7+8+10+10 = 41

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

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes


  • Dec 16th, 2014

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

  Was this answer useful?  Yes


  • Dec 16th, 2014


  Was this answer useful?  Yes


  • Dec 16th, 2014

case ii is correct but not case i

  Was this answer useful?  Yes

anshul maheshwari

  • Dec 27th, 2014

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

  Was this answer useful?  Yes


  • 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

  Was this answer useful?  Yes


  • Mar 12th, 2015


  1. a++ + ++a + a++

  2. a = 5

  3. a++ = 5

  4. ++a = 7

  5. ++a = 7

  6. so 5 + 7+ 7 = 19

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

brajesh soni

  • Mar 26th, 2015

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

  Was this answer useful?  Yes


  • Jun 2nd, 2015

  1. if a =5;

  2. c=++a + ++a + ++a;

  3. c = 6+7+8 = 21;


  5. if a =5;

  6. c=a++ + a++ + a++;

  7. c=5+6+7 = 18;

  Was this answer useful?  Yes


  • Jun 2nd, 2015

  1. if a =5;

  2. int c = ++a + ++a + ++a;

  3.              |          |         |

  4.             6           7        8     ; value of a becomes  8;

  5. c = 6+7+8 = 21;



  8. if a =5;

  9. int c = a++ + a++ + a++;

  10.              |          |         |

  11.             5           6       7     ; value of a becomes 7;

  12. c = 5+6+7 = 18;

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes


  • Aug 3rd, 2015

6+7+8 = 21...

  Was this answer useful?  Yes


  • Nov 17th, 2015

Totally compiler dependent !

  Was this answer useful?  Yes

Nitin Garg

  • Dec 12th, 2015

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

In this equation first i increment by 1
then again increment by 1
then add first two is (++i + ++i) so addition is 14
then again increment by 1
then add (14 + ++i) so addition is 22

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes


  • Aug 10th, 2017

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

  Was this answer useful?  Yes

@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.

  Was this answer useful?  Yes


  • 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.)

  Was this answer useful?  Yes

Anil kumar

  • Mar 25th, 2019

it is study of prefix behaviour.
i got it
if a=5,b=0;
it will like
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

  Was this answer useful?  Yes


  • Jun 27th, 2019

It is very easy
C=++a + ++a + ++a;
C=a++ + a++ + a++;

  Was this answer useful?  Yes


  • Aug 18th, 2019

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

  1. #include <iostream>

  2. using namespace std;


  4. int main() {

  5.         int a=5,b;

  6.         b=++a + ++a + ++a;

  7.         cout<<b;

  8.         return 0;

  9. }

  Was this answer useful?  Yes


  • 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

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions