First, you must read What is a programming language, IDE and compiler?.
If you think you should give another value you should justify it. Programming is justification, otherwise it turns to bullshit.
You took a undefined behavior. So don’t do it, there’s no need. You have a compiler who won’t even let you compile it if you don’t force it. Don’t use operators you own side effects, as is the case with the increment (++
) along with other expressions, anything can happen without the code being wrong, and therefore you have no control over it. If you really want to make the increment then do it in a separate line and control the exact moment that should happen.
#include <stdio.h>
int main (){
int i = 5, j = 3, *p = &i, *q = &j, x = (*p)++;
printf("%d %d \n", *p, x + *q);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
So you have control and give the result you got, that is, the compiler chose to make the increment first and then do the rest, which is what I would do since the increment operator takes precedence over the others, including the comma that has less precedence, then it runs before everything and then uses the result already obtained. Although the expected result could be achieved on some compiler or platform, it would make less sense.
In fact 5 and 9 I can’t even imagine why it would, you should really justify it.
And you can’t change anything in the code, think it looked like it, and then give it the expected result, another code is another code. A code that has no side effect will give different result, are completely different operations.
"I believe the IDE is not reading correctly" - You haven’t said which IDE you’re using, but unless it’s an unknown medium, it’s unlikely it’s her problem. IDE’s have been used for decades by millions of professionals around the world to make complex and diverse programs, so what are the chances that by doing a simple exercise you have encountered a serious problem with such a basic functionality? I’m not saying it’s impossible, but it’s extremely improbable. "Never" (or "almost never") is the fault of the IDE, compiler or language. Almost always the fault of the programmer.
– hkotsubo
Anyway, as it is an undefined behavior, the result may vary according to the compiler. Just to name two examples, in Ideone.com (using gcc 8.3) the result was
6 8
and in Repl.it (using Clang-7) the result was5 8
. And if college says the answer is "alwaysx y
", question and ask why no one has explained that this is indefinite behavior...– hkotsubo