Increment does not work as expected

Asked

Viewed 64 times

-1

I took this exercise and put it to compile, but it’s going wrong I think because the values I see would be 5 and 9, but it’s going 6 and 8, I believe the IDE is not reading correctly.

#include <stdio.h>

 int main (){
 int i = 5, j = 3, *p, *q;
 p = &i;
 q = &j;
 printf("%d %d \n", *p, (*p)++ + *q);
    return 0;
    }
  • "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.

  • 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 was 5 8. And if college says the answer is "always x y", question and ask why no one has explained that this is indefinite behavior...

2 answers

1


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.

  • Interesting the texts, I do not know right about pointers I am learning in college, this is an exercise of her to speak what value would give, I did not know that I had problems as what you said, but thanks for the instructions Manieri

1

#include <stdio.h>

 int main (){
 int i = 5, j = 3, *p, *q;
 p = &i;
 q = &j;

 printf("%d %d \n", *p,(*p + 1 + *q ));
    return 0;
    }

I did it this way, and it came out what you wanted ... it’s kind of a weird way, but it was

  • face the code is the same almost, at least in the part that yours of the 5, why does that happen

  • @Cl2727 Because - as already explained the other answer - an expression that has the operator ++ along with other operations generates undefined behavior. Removing the ++, the undefined behavior disappears, since now the value of *p is no longer changed during the evaluation of the expression.

  • Got it, thanks hkotsubo!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.