# Output of a++ + ++a + a++

What is the answer of a++ + ++a + a++

int a=5;

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

//How

#### austin1030 Profile Answers by austin1030

• Feb 11th, 2008

given a = 5;

first a++ gives 5 but increment by 1 after accessing variable 'a'

then, ++a give 7 since 'a' is 6 after first access

last, a++ give another 7 because it does not increment not until 'a' access

so , c = 5 + 7 + 7 = 19

#### anu1985 Profile Answers by anu1985

• May 31st, 2008

The out out of expression when a=5 is 19

#### windows_pal Profile Answers by windows_pal

• Aug 17th, 2008

a=5
5+6+6=17

#### Tarkeshwar_Prasad Profile Answers by Tarkeshwar_Prasad Questions by Tarkeshwar_Prasad

• Mar 16th, 2009

The output of a++ + ++a + a++ will be 18. First operation takes place is ++a that will be 5+1=6 and then c=x+x+x, c=6+6+6=18 after the output of c post operations of x will take place.

#### gamepaln22sep Profile Answers by gamepaln22sep

• Jun 24th, 2009

Here the value of a=8 if a=5;
and c=18 if c=a++ + ++a + a++
because a will evaluate thrice so a=8 and for c first take a++ + ++a here 5+6=11
and one post increment of a
so a=7 again 11+a++=11+7=18 and one a++=8;

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

• Feb 1st, 2011

From the C Language standard, section 6.5:

----------
2.  Between the previous and 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;
-------------

The expression "a++ + ++a + a++" violates this restriction, so the behavior is undefined; any result is considered "correct", whether it's 16, 18, 28, 42, a suffusion of yellow, etc.  The compiler isn't even obligated to finish translating the program.

Exactly how a particular result is produced will vary from platform to platform, and may depend on everything from the surrounding code to compiler options.

#### Srikanth Reddy Sarabudla Profile Answers by Srikanth Reddy Sarabudla

• May 23rd, 2011

Follow the same rule for the second equation by breaking it down.
a++ (a is now 5, does it appear before +, yes then return 6).
equation is now 6 + ++a + a++
++a (a is now already 6 because was incremented in the previous equation so this returns 7 because 6++, but remember it does not return to the original value as it is already as it is incremented on the spot and a remains 6)
equation is now 6 + 7 + a++
a++ (a is now 6, does i appear before +, no then return 6 as it is not incremented yet.
equation is 6 + 7 + 6 = 19.

#### Ravi Kanth Manne Profile Answers by Ravi Kanth Manne

• May 27th, 2011

#### jaspreet kaur

• Jul 14th, 2011

in unix enviornment its answer is 18.

#### satya

• Jul 22nd, 2011

hey...... it wont print 28 in any programming language. please try again. It will print 18 in C/c++ and java it will print 19.

#### GARGI GANGULY

• Sep 10th, 2011

At First a is incremented a=6; but its a post increment so value count as a 5. then a is used as pre increment the value become 7 and last one is also 7, because it is also post increment. So the format is c=7+7+5. Output is 19.

#### rinsdominic Profile Answers by rinsdominic

• Dec 28th, 2011

The precedence of unary operator ++ is from right to left
therefore first evaluate the section ++a + a++
ie, from right side value of a is 5 and in left value of a is 6 because preincrement and calculate the value 6+5=11
then perform the postincrement therefore value of a become 7 then perform the rest in the left portion
ie the current value of a is added with the intermediate result ie, 7+11= 18 .. then perform the post increment of a therefore a become 8
try this

```Code#include<stdio.h>
int main()
{
int a=5,c;
c = a++ + ++a + a++;
printf("Value of a= %d",a);
printf("
value of c= %d",c);
return 0;
}```

• Dec 29th, 2011

Sorry for the first misunderstanding....
First evaluate the section a++ + ++a ie, from left side value of a is 5 and in right value of a is 6 because preincrement and calculate the value 5+6=11 then perform the postincrement of left a therefore value of a become 7 then perform the rest in the right portion ie the current value of a is added with the intermediate result ie, 11+7= 18 .. then perform the post increment of a therefore a become 8 try this

```Code
#include<stdio.h>
int main()
{
int a=5,c;
c = a++ + ++a + a++;
printf("Value of a= %d",a);
printf("
value of c= %d",c);
return 0;
}```

#### vamshi

• Jan 27th, 2012

How means : It first performs pre-increment i.e a=6, now it performs addition i.e 6+6+6=18, than it has to perform post increment so the value of a is 20.

#### ankur

• Feb 1st, 2012

for a++ + ++ a

first a value will become 6 so addition would be 12 now since a is already 6 it value will increase to seven then addition will take place so now addition will be 12+7=19 and the value is a is 8 because of increment after evaluation in case of a++

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

• Feb 4th, 2012

Just to drive home the point I made earlier...

The order in which operands are evaluated and side effects are applied is *unspecified*; given a statement like "y = x++ + ++x", theres no guarantee that "x++" is evaluated before "++x", or that the side effects of the two expressions are applied in any specific order.

Ive written a small C program that compares the results of "y = x++ + x++;" to "y = x++; y = y + x++;". If the ++ operator behaved the way everyone else has been describing, the results *should* be the same. But if you look at the results of the program (attached below as part of the code snippet), youll see that in many cases, the results *arent* the same.

Again, expressions of the form "x++ + x++" *will* invoke undefined behavior, and the results will vary based on your compiler, any optimization settings, surrounding code, etc. This code was built on Ubuntu 10.04 using gcc 4.4.3 on an AMD x86 processor.

```Code/************************** undef.c **************************/

#include <stdio.h>

#ifndef START_VAL
#define START_VAL 0
#endif

#ifndef AUTOOP1
#define AUTOOP1 ++
#endif

#ifndef AUTOOP2
#define AUTOOP2 ++
#endif

#ifndef OP
#define OP +
#endif

#define EXPAND(x) x
#define PASTE(a,b) EXPAND(a)b

#define POSTIFY(x,y) PASTE(x,y)
#define PREIFY(x,y) PASTE(y,x)

#if defined(DIR1)
#if DIR1==0
#define AUTO1(x) PREIFY(x,AUTOOP1)
#else
#define AUTO1(x) POSTIFY(x,AUTOOP1)
#endif
#else
#define AUTO1(x) POSTIFY(x,AUTOOP1)
#endif

#if defined(DIR2)
#if DIR2==0
#define AUTO2(x) PREIFY(x,AUTOOP2)
#else
#define AUTO2(x) POSTIFY(x,AUTOOP2)
#endif
#else
#define AUTO2(x) POSTIFY(x,AUTOOP2)
#endif

#define STRINGIFY(x) #x
#define STREXPAND(x) STRINGIFY(x)
#define AUTOSTR(x) STRINGIFY(x)

int main(void)
{
int x = START_VAL;
int y;

printf("%-20s%8s%8s\n", "Statement", "    y", "    x");
printf("%-20s%8s%8s\n", "---------", "-----", "-----");

/* well-defined */
y = AUTO1(x);
y = y OP AUTO2(x);
printf("%-20s   %5d   %5d\n", "y = " AUTOSTR(AUTO1(x)) "; y = y " AUTOSTR(OP) " " AUTOSTR(AUTO2(x)), y, x);

/* undefined */
x = START_VAL;
y = AUTO1(x) OP AUTO2(x);
printf("%-20s   %5d   %5d\n\n", "y = " AUTOSTR(AUTO1(x)) " " AUTOSTR(OP) " " AUTOSTR(AUTO2(x)), y, x);
return 0;
}

/************* permute.sh **************/
#!/bin/sh

for a1 in ++ --
do
for d1 in 1 0
do
for a2 in ++ --
do
for d2 in 1 0
do
for op in + -
do
gcc -o undef undef.c -DAUTOOP1=\${a1} -DAUTOOP2=\${a2} -DDIR1=\${d1} -DDIR2=\${d2} -DOP=\${op} -DSTART_VAL=0
./undef
done
done
done
done
done

/************************ Results ***************************/

Statement                  y       x
---------              -----   -----
y = x++; y = y + x++       1       2
y = x++ + x++              0       2

Statement                  y       x
---------              -----   -----
y = x++; y = y - x++      -1       2
y = x++ - x++              0       2

Statement                  y       x
---------              -----   -----
y = x++; y = y + ++x       2       2
y = x++ + ++x              2       2

Statement                  y       x
---------              -----   -----
y = x++; y = y - ++x      -2       2
y = x++ - ++x              0       2

Statement                  y       x
---------              -----   -----
y = x++; y = y + x--       1       0
y = x++ + x--              0       0

Statement                  y       x
---------              -----   -----
y = x++; y = y - x--      -1       0
y = x++ - x--              0       0

Statement                  y       x
---------              -----   -----
y = x++; y = y + --x       0       0
y = x++ + --x             -2       0

Statement                  y       x
---------              -----   -----
y = x++; y = y - --x       0       0
y = x++ - --x              0       0

Statement                  y       x
---------              -----   -----
y = ++x; y = y + x++       2       2
y = ++x + x++              2       2

Statement                  y       x
---------              -----   -----
y = ++x; y = y - x++       0       2
y = ++x - x++              0       2

Statement                  y       x
---------              -----   -----
y = ++x; y = y + ++x       3       2
y = ++x + ++x              4       2

Statement                  y       x
---------              -----   -----
y = ++x; y = y - ++x      -1       2
y = ++x - ++x              0       2

Statement                  y       x
---------              -----   -----
y = ++x; y = y + x--       2       0
y = ++x + x--              2       0

Statement                  y       x
---------              -----   -----
y = ++x; y = y - x--       0       0
y = ++x - x--              0       0

Statement                  y       x
---------              -----   -----
y = ++x; y = y + --x       1       0
y = ++x + --x              0       0

Statement                  y       x
---------              -----   -----
y = ++x; y = y - --x       1       0
y = ++x - --x              0       0

Statement                  y       x
---------              -----   -----
y = x--; y = y + x++      -1       0
y = x-- + x++              0       0

Statement                  y       x
---------              -----   -----
y = x--; y = y - x++       1       0
y = x-- - x++              0       0

Statement                  y       x
---------              -----   -----
y = x--; y = y + ++x       0       0
y = x-- + ++x              2       0

Statement                  y       x
---------              -----   -----
y = x--; y = y - ++x       0       0
y = x-- - ++x              0       0

Statement                  y       x
---------              -----   -----
y = x--; y = y + x--      -1      -2
y = x-- + x--              0      -2

Statement                  y       x
---------              -----   -----
y = x--; y = y - x--       1      -2
y = x-- - x--              0      -2

Statement                  y       x
---------              -----   -----
y = x--; y = y + --x      -2      -2
y = x-- + --x             -2      -2

Statement                  y       x
---------              -----   -----
y = x--; y = y - --x       2      -2
y = x-- - --x              0      -2

Statement                  y       x
---------              -----   -----
y = --x; y = y + x++      -2       0
y = --x + x++             -2       0

Statement                  y       x
---------              -----   -----
y = --x; y = y - x++       0       0
y = --x - x++              0       0

Statement                  y       x
---------              -----   -----
y = --x; y = y + ++x      -1       0
y = --x + ++x              0       0

Statement                  y       x
---------              -----   -----
y = --x; y = y - ++x      -1       0
y = --x - ++x              0       0

Statement                  y       x
---------              -----   -----
y = --x; y = y + x--      -2      -2
y = --x + x--             -2      -2

Statement                  y       x
---------              -----   -----
y = --x; y = y - x--       0      -2
y = --x - x--              0      -2

Statement                  y       x
---------              -----   -----
y = --x; y = y + --x      -3      -2
y = --x + --x             -4      -2

Statement                  y       x
---------              -----   -----
y = --x; y = y - --x       1      -2
y = --x - --x              0      -2
```

#### govind bang

• Feb 6th, 2012

i think answer is 20 bcoz a++ means a+1
so a++ = 6

#### Asarudeen

• Feb 9th, 2012

C = a++ + ++a + ++a

Compiler starts from leftmost operand

```Code1+5 = 6

a = 6

1+6 =7

a = 6+7

a =13

1+13 =14

Now

c = 6+ ++7 ++13

c = 28```

#### peter

• Mar 25th, 2012

Which operation in the following expression will be performed first?

c = a++ / b + 5;

#### Arvind singh

• Aug 17th, 2013

`Code18`

#### phani

• Jan 3rd, 2014

c=19 and a=8

#### anudeep jaini

• Feb 18th, 2014

19

#### piyush

• Feb 3rd, 2015

int a=5,c;
c = ++a + ++a + a++;
then c=?

#### pushpendra

• Feb 12th, 2015

18, because in modern compiler . first we did pre increment so a = 6 , add all c = 18, after executing this instruction a = 8

#### Niranjan

• Feb 19th, 2015

20

• Aug 17th, 2015

Hi,
What should be ans of the following:
int i = 5;
a = ++i + i++;
printf("%d", a);
?
Compiler says 13. But, how?

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

• Aug 27th, 2015

The answer can quite literally be *anything*; the behavior is undefined. With few exceptions, the order in which expressions are evaluated and in which side effects are applied is *unspecified*; you cannot assume that expressions are evaluated left to right, and you cannot assume that the side effects of the ++ and -- operators are applied immediately after evaluation. The C language standard explicitly calls expressions like this out as undefined behavior.

#### neha

• Sep 2nd, 2015

It is 19
Evaluate like this:- 5++ + 7 + 7++
First is post increment so 5 is printed first then increment is Performed over a

Now a=6
on pre-increment it becomes 7
Again a post increment is done in which 7 is printed and a becomes 8 now

so, 5+7+7=19

#### Rahul Gupta

• Sep 12th, 2015

There are 2 type of operator is involve in this expression firs is unary increment which rank is higher than second operator binary addition So, first increment is solved from right to left because is associativity is right to left.
Then after perform the addition and one thing more it is a single statement for computer so don't break it in multiple lines
int a=5,b;
b=a++ + ++a + a++;
solution is
a++ + ++a + 5 because of a++ is post fix so it participate in exp with its current value
a++ + 6 + 6 because ++a is prefix so it increase 1 value and second 6 is because used same variable
6+6+6
so the result will be 18.

#### Praveen

• Sep 14th, 2015

The answer is 18, because first pre-increment happens then expression is evaluated and then post increment occurs. so first a=6 >> (6++)+6+(6++) = 18 and finally a=8

#### sandeep singh rawat

• Oct 13th, 2015

The logic behind such problems is associativity and precedence and it is easy to learn also PUMA SRE BIT3 LOG2 TAC. I did not provide the solution to this problem because there are so may questions like this you can put and getting the solution will not teach you anything.

#### Ripon Hossain Jewel

• Nov 24th, 2015

```Code    #include<stdio.h>
int main()
{
int a=5,c;
c = a++ + ++a + a++;
printf("Value of c= %d",c);
return 0;
}```

#### khasim

• Nov 26th, 2015

18

#### vijayanandhini mano

• Dec 12th, 2015

19

#### Alejandro Visiedo

• Mar 5th, 2016

The output is not 28, its 19.

#### rakesh

• Jun 22nd, 2016

{
i=5;
a=++i + i++; // 6 + 7
printf("%d",a); //13
}

#### priya

• Jun 29th, 2016

19

#### Praveen S

• Aug 7th, 2016

Answer is 22 not 28 .

```Code+ does addition for 2 at a time :

its (a++ + ++a) + a++;
thus answer is (7 + 7) + 8 = 22 //Not 28 .```

#### bhargav

• Aug 30th, 2016

given int a=5;
if a=5 then
value of a value of a value of a value of a
initial after a++ after ++a after a++
a=5 a++=5 ++a=7 a++=7
a=6 now a=7 now a=8 now
then the sattement a++ + ++a +a++=5+7+7=19

#### Ashish

• Mar 15th, 2017

a++ + ++a + a++
5+6 + 1+5 + 5+6
11 + 6 + 11
=28
It is my answer, correct me if wrong

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

• Jun 9th, 2017

The answer is that the behavior of "a++ + ++a + a++" is *undefined* - there is no right answer (or, more properly, any result is equally correct). Except in a few cases, C does not force left-to-right evaluation, nor does it require that the side effect of the ++ operator be applied immediately after evaluation. So if x is initialized to 5, then the expression can be evaluated as "5 + 6 + 5", or "5 + 6 + 6", or "6 + 7 + 5", or "5 + 7 + 7", or pretty much anything else.
From the C language standard (section 6.5, para 2): "If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined."
None of the expressions a++, ++a, and a++ are sequenced with respect to each other. Each has a side effect on a. Per the clause above, the behavior is undefined. Youll get *a* result, it just may not be what you expect.

#### rajat

• Sep 25th, 2017

According to precedence of ++ postfix and prefix operators postfix ++-- are executed prior to prefix ones

#### Pramodh Venkatraman

• Dec 18th, 2017

++a gives the value of i as 6 and i++ gives the value of 7.
++5 + 6++ = 6+7=13.

#### Sharath

• Sep 13th, 2019

int a=5,b;
b=a++ + ++a;
after this a=7,b=12
int a=5,b;
b=++a +a++;
after this a=7,b=13
Can you explain how come it is ?

#### susruth

• Sep 25th, 2019

```Code#include<stdio.h>
int main()
{
int a=5;
int k= --a + a--;
printf("k= %d   a=%d
".k,a);
return 0;
}```  