0

I would like to know the reason why the two platforms behave inconsistently

#include <stdio.h>

int main()
{
    int count = 0;
    count = count++;
    printf("%d\n",count);  //linux count=0; windows count = 1; why? 
    return 0;
}
  • 1
    To quote from the duplicate, this is a textbook example of undefined behavior. – kotatsuyaki Nov 27 '22 at 14:42
  • Note that both platforms produce acceptable answers. The result is undefined by the standard (the result of undefined behaviour), so any result is okay. – Jonathan Leffler Nov 27 '22 at 14:46
  • Two questions: (1) Do you have an opinion on which of the two compilers is "correct"? (2) Would you *ever* write `count = count++` in a real program? For me, at least, the answers are emphatically "no" and "no". – Steve Summit Nov 27 '22 at 15:21
  • 1
    An insane person said to his two friends: "I do not not not not not not want to go to the party with you." Friend #1 said, "Okay, so you do want to come! Great!" Friend #2 said, "Oh, well, sorry to hear you won't be coming." Which friend interpreted the insane person's statement correctly? – Steve Summit Nov 27 '22 at 15:24
  • When you compiled it on Linux (presumably using gcc or clang), did you notice the compiler warning saying something like "warning: operation on 'count' may be undefined"? That should be a really big hint. (Your Windows compiler may well have warned also, but I don't have one to test.) Don't ignore compiler warnings! – Nate Eldredge Nov 27 '22 at 18:15

0 Answers0