-1

I came across this code:

#include<iostream>
using namespace std;
int main()
{
    int a=5,
    b=a++ - 2;
    cout << b;
    return 0;
}

The output is 3. Why is it not 4?

DV82XL
  • 5,350
  • 5
  • 30
  • 59
muneebbug
  • 9
  • 3
  • 3
    *the code gives the same output*. What output? The code shown does not output anything. The second expression expands to `b=a-2; a=a+1;`. Because `a++` is a post-increment which means use the value of `a` in the expression then increment it. – kaylum Jan 15 '21 at 10:31
  • 1
    @web_surgeon How is the name Int defined? – Vlad from Moscow Jan 15 '21 at 10:34
  • I mean it gives me 6 output when i cout it in c++. – muneebbug Jan 15 '21 at 10:35
  • 1
    @web_surgeon And what output did you expect? – Vlad from Moscow Jan 15 '21 at 10:35
  • 1
    cout what? Please show the exact code as a [complete minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) – kaylum Jan 15 '21 at 10:35
  • Please check the post again! i edited it. – muneebbug Jan 15 '21 at 10:38
  • I just want to know what does the '-2' it gives me same output with -2 and without -2 – muneebbug Jan 15 '21 at 10:39
  • 1
    It gives `3` with the `-2`: https://ideone.com/iSmQKS and `5` without it: https://ideone.com/ZsoWgl Your `#include ` implies that you are using a very very very old compiler, you should use one from this millenium. – mch Jan 15 '21 at 10:41
  • Yeah i just realized. Thank you – muneebbug Jan 15 '21 at 10:43
  • This code means that you assign to `b` result of expression `a++ - 2`, you should clarify what you do not understand. How `a++` works? How `value - 2` works? Something else? – Slava Jan 15 '21 at 17:01

2 Answers2

1

-2 or - 2 should not give any error see there are two types of operator post increment and pre increment a++ is post increment it means first it will assign the value then it will increase the value by 1

meaning b = 5 - 2; a will get get increased by 1 a=6 now but in the equation it will be 5

but if you do ++a

then it will first increase the value then assign meaning b = 6 - 2;

-2 or - 2 wont give any error

check here

Mohak Gupta
  • 163
  • 1
  • 9
0

Lets break it down step by step.
These type of comma separated expressions happen from left to right. So its the same as this,

int a = 5;
int b = a++ - 2;

In this, a++ increments the value of a by one and then assigns it to it. Then the - 2 happens. Simply what happens under the hood is,

// Here 5 is the value of a.
int b = 5 - 2, a = a + 1;

More info: Incrementing in C++ - When to use x++ or ++x?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
D-RAJ
  • 3,263
  • 2
  • 6
  • 24
  • 1
    Thank you! I understand now – muneebbug Jan 15 '21 at 10:41
  • Think you meant the right thing, but the way of explaining this can be confusing. It does NOT increment by one and then applies -2 to it. If that were the case, the result would be 4. It applies -2 to the original value (5) so we get 3. And THEN it increments "a" by one. – AlexGeorg Jan 15 '21 at 12:08
  • @AlexGeorg Yup I knew it would be a little confusing. That's why I gave a little snippet. – D-RAJ Jan 15 '21 at 12:16