-1
#include<stdio.h>
int main()
{
    int x=2;
    x=x++;
    printf("%d",x);
    return 0;
}

Output according to my logic : 2

Output on windows : 3

Output on Linux: 2

Why is windows giving 3 as output. From what I understand x++ increments 2 to 3 but return back 2. So x should have 2.Is windows evaluating this anything different.

Similarly:

#include<stdio.h>
 int main()
 {
    int x=2,y=4;
    x=x++ + ++y;
    printf("%d %d",x,y);
    return 0;
 }

Output according to me : 7 5

Output in windows : 8 5

Output on Linux : 7 5

Again the same situation.

Please help.....

1 Answers1

0

x = x++ is undefined behavior so the two compilers are generating two different pieces of code.

Just x++ should suffice for your first piece of code.

Here is a question with your exact problem for the second piece of code

Community
  • 1
  • 1
Leroy
  • 237
  • 2
  • 12