0
int main()
{
    int a = 1, b = 1, c = 1;
    int x = a + a++ ; 
    int y = 1 + b++ ; 
    int z = c++ + c++ ; 
    printf("%d\n %d\n %d\n", x, y, z);
}

Output:

   3
   2
   3

I know there are similar questions out there but nothing addressed my issue. I'm basically doing the same thing in 2nd & 3rd lines of the main function . Why do I receive different outputs?

And all the materials I referred to so far said that post increments take place after the operations have taken place. In that case, shouldn't we assign z to c+c(i.e 1+1=2) and then increment the value of c?But clearly my logic is wrong. Can someone explain that too?

  • Undefined behaviour. Move on, or start coding in Java. – Bathsheba Feb 13 '20 at 17:20
  • 1
    Does this answer your question? [Why are these constructs using pre and post-increment undefined behavior?](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) – Thomas Jager Feb 13 '20 at 17:20
  • @Thomas Jager I thought languages were perfect & kinda didn't know such a thing as undefined behavior existed. Thanks for the suggestion. – Samyuktha0511 Feb 13 '20 at 17:26
  • @Bathsheba I guess I'm moving on.. :| – Samyuktha0511 Feb 13 '20 at 17:27
  • 1
    Don't do that! You won't get paid as much! C is perfect insofar that it's multiplatform and blisteringly fast if used properly so it omits various useless bits of functionality that would otherwise kill runtime performance. – Bathsheba Feb 13 '20 at 17:29
  • @Bathsheba Thats interesting.Maybe I should stick to C until i master the nitty gritties Thanks dude:) – Samyuktha0511 Feb 13 '20 at 17:53

0 Answers0