## 8 replies

Last post Jan 08, 2014 06:06 PM by Paul Linton

• arry.net

Member

430 Points

221 Posts

### why the output is 42 ?

int a = 20;

a++; //21

a += a--; //21+21 what about --?

Console.WriteLine(a);

why the output is 42 ? why not 41

operator C operatorprecedence

arry.net
• Rion William...

All-Star

113020 Points

18361 Posts

MVP

### Re: why the output is 42 ?

Dec 20, 2013 12:12 PM|Rion Williams|LINK

This is because these are postfix operations, so they are going to output the value of the variable prior to your increment or decrement statements.

As per the MSDN documentation

The `++` and `--` operators also support postfix notation, (Section 7.5.9). The result of `x++` or `x--` is the value of `x` before the operation, whereas the result of `++x` or `--x` is the value of `x` after the operation. In either case, `x` itself has the same value after the operation.

You can see a working example demonstrating this below :

```//Declare your integer value
int a = 20;

//Increment it
Console.WriteLine(a++);

//Now you'll see that your value is 21
Console.WriteLine(a);

//The following line is going to output (21) + (21)
Console.WriteLine(a += a--);```

If you really wanted to output 41, you would need to use postfix notation instead which would output the value after your increment or decrement operation occurred by changing this line :

`a += a--;`

to this :

`a += --a;`

operator C operatorprecedence

• A2H

All-Star

50471 Points

9785 Posts

### Re: why the output is 42 ?

Hi,

You may need to use prefix decrement operation:--a like given below.The result of the operation is the value of the operand "after" it has been decremented

`a += --a;`

Working Sample Demo

```
int a = 20;

a++; //21

a += --a; //21+21 what about --?

Console.WriteLine(a);```

Further Details

The decrement operator (--) decrements its operand by 1.

The decrement operator can appear before or after its operand:

--variable

This form is a prefix decrement operation. The result of the operation is the value of the operand "after" it has been decremented.

variable--

This form is a postfix decrement operation. The result of the operation is the value of the operand "before" it has been decremented.

operator C operatorprecedence

Thanks,
Aje
My Blog | Dotnet Funda
• arry.net

Member

430 Points

221 Posts

### Re: why the output is 42 ?

but  the code is

a += a--; //after semicolon the -- must be evaluate ?

Console.WriteLine(a);

after semicolon the -- must be evaluate ?

operator C operatorprecedence

arry.net
• A2H

All-Star

50471 Points

9785 Posts

### Re: why the output is 42 ?

Hi,

a++ is an expression which returns the current value a(20), then increments it. Therefore,first it will evaluate a, increment a, then assign a to the original value, before it was incremented.

```//Assign the value to variable
int a = 20;
//Increment the variable with ++(1)
//If you see here first its assign the value 20 to variable b and then only increment which we receive as 21 in
//below sections
int b = a++; //21
Console.WriteLine(b);

// if we take this case first it will use the value 21 to the += (21 +21)
//operation and then only it will reduce the value
a += a--; //21+21 what about --?
Console.WriteLine(a);
Console.WriteLine(" *****************************");
//Get the value from previous operation
//You can see that still it hold the value 42
int c = a--;
Console.WriteLine("Current Value before assignment : " +c); //Outputs  42
//Get the value from previous operation
//Here you can see that the value is decremented by 1
//ie value is decremented by one after assigning
int d = a;
Console.WriteLine("Current Value after assignment : " + d); //Outputs 41```

You can check this demo for more deatails Working Sample Demo

operator C operatorprecedence

Thanks,
Aje
My Blog | Dotnet Funda
• Paul Linton

Star

9555 Points

2784 Posts

### Re: why the output is 42 ?

Dec 21, 2013 06:34 PM|Paul Linton|LINK

Your  question shows a few of the subtleties in the postfix operators (I don't know why people are talking about prefix, the operators are clearly after the argument).

You have a mistake in your comment which is instructive.  This ilne can be considered to be incorrect

a++;//21

++ is an operator.  Operators operate on their arguments and return a result.  The result of a++ is the value of a before it is incremented.  So, you could argue that a better commented line would be

a++;//20

I admit that most people would call that misleading.  Consider, this slight variation

int b = a++; //20

The value of b will be 20 which matches the comment.

The postfix operator ++ has a side-effect and that is to increase the value of a by one.

To paraphrase the C# standard.

```The run-time processing of a postfix increment operation of the form a++ consists of the following steps
- The value of a is saved
- The increment operator is invoked with the saved value of a as its argument
- The value returned by the operator is stored in a
- The saved value of a becomes the result of the operation```

(I don't intend to confuse things but for the prefix operator, ++a, the only difference is in the last step, change the word 'saved' to 'new'.  In particular, everything happens in exactly the same order.  Check out Eric Lippert's answer to http://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i (not the first answer, it is wrong.  The answer given by Eric is correct)

a += a--;

As just discussed the value returned by a-- is the original value of a, that is 21.  But there is a new question, "is the value of a on the lhs taken before or after the rhs is evaluated"?  Some may think that the order of operation is to evaluate the rhs and to then evaluate the lhs which would be the equivalent of

```int c = a--;
a += c;```

THIS IS NOT WHAT HAPPENS.  The entire statement is evaluated left to right.  The special behaviour of the += operator is that the lhs is only evaluated once.  The lhs is evaluated and saved temporarily.  The rhs is evaluated and the result is added to the saved value before storing back to the original variable.

```a += a--;
1) a (on the left hand side) is evaluated and saved in a temporary location (that is, 21 is saved)
2) the rhs is evaluated and the returned value is 21, (as a side effect a is changed to 20, but that is irrelevent and never used).
3) the returned value (21) is added to the saved value (21) from step 1, giving 42
4) the computed value (42) is saved as a```

operator C operatorprecedence

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
• arry.net

Member

430 Points

221 Posts

### Re: why the output is 42 ?

my confusion was why a-- didn't get evaluated

i got it now, Thanks paul  :)

operator C operatorprecedence

arry.net
• arry.net

Member

430 Points

221 Posts

### Re: why the output is 42 ?

Actually i found some more intrested things.

int c = 5;
c = c++ + c++ + c++; /5+6+7 // evalulates after the operator / last one is always missed.
Console.WriteLine(c);

int b = 5;
int a = 0;
a = b--+b++ ;  // evalulates after the operator / last one is always missed.
Console.WriteLine(a);//9

operator C operatorprecedence

arry.net
• Paul Linton

Star

9555 Points

2784 Posts

### Re: why the output is 42 ?

Jan 08, 2014 06:06 PM|Paul Linton|LINK

In a previous reply I gave you the detail of what happens when the postfix ++ operator is applied.  You should be able to apply that knowledge to these cases.  There is nothing special happening.

Think about it step by step.

C++ + c++ + C++

Evaluate the first c++, what is the result, what is the side effect?

Evaluate the second c++, what is the result, what is the side effect?

Evaluate the third c++, what is the result, what is the side effect?

Add the three results and then assign that to the variable on the LHS.

Exactly the same with your second example

b-- + b++

What is the result of b--?  What is the side effect?

What is the result of b++?  What is the side effect?

Add the two results and then assign that to the variable on the LHS.

operator C operatorprecedence

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.