0

The output of the following code

int k=10;
int g=5;
int s=g+k++;
cout<<s;

is 15

but the output of the following code

int k = 10;
int s= ++k + k++;
cout << s;

is coming as 23. What is the correct explanation? this was doubt number #1

doubt number #2 is

int m = 10;
int n = 10;
int a = 1 + (++m) + (++(++m))++;
int b = (++n) + (++(++n))++;

how is the value of both a and b the same which is 27

user207421
  • 305,947
  • 44
  • 307
  • 483
ZubinShah
  • 11
  • 1
  • 4
    I believe the answer is that you're modifying `k` twice without an intervening sequence point, so the code has undefined behaviour. But this isn't the right site to ask technical C++ questions. – Thomas Lumley May 09 '21 at 05:05
  • which site is better then – ZubinShah May 09 '21 at 05:35
  • 3
    This kind of question gets asked pretty often. [Here's](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) a dupe. – Nathan Pierson May 09 '21 at 06:35
  • There is no [tag:linear-programming] here, and practically everything in computer programming involves [tag:logic]. Don't tag indiscriminately. – user207421 May 09 '21 at 07:11

0 Answers0