-4

The output of:

#include <stdio.h>


int Fun1(int *X)
{
    return (*X)++;
}


int main(void)
{
    int a = 0, b = 5;
    a = Fun1(&b);
    printf("a = %d.  b = %d.\n", a, b);
}

is:

a = 5.  b = 6.

When we return (*X)++, should not the function execution stop when we return (*X), so the ++ is not executed?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • The function execution stops after the expression it returns is evaluated. The expression it returns is `(*x)++`. – bereal May 27 '21 at 09:34
  • 3
    Maybe but `*X = (*X)++ + ++(*X)` smells pretty UB. – Déjà vu May 27 '21 at 09:35
  • I know it is a bad code. it is a university exam where we should predict the output. – Mohamad Jawad Merhi May 27 '21 at 09:39
  • 1
    Mohamad, I think your actual question has been lost in the mess of this code. (That's why stack-overflow encourages a _minimal_ example of the code you're asking about). I think you were asking when you have `return (*x)++;` whether `*x` will be incremented or not. It will. Section 6.5.2.4 of the standard says that the update of `*x` occurs between the previous and the next sequence point. Appendix C lists the "at the end of ... an expression in a return statement" as a sequence point. – Paul Hankin May 27 '21 at 09:58
  • I reduced your code to a minimal example and reopened the question. I also removed ``, since that is not a C standard header. Dynamic memory allocation was not necessary for this question, so I removed it rather than using the standard header, ``. Also, `main` should be declared to return `int`, not `void`, and should be declared as `int main(void)` or `int main(int argc, char *argv[])` or other forms specifically defined by the C implementation. – Eric Postpischil May 27 '21 at 10:22
  • @EricPostpischil Hmmm, I'm not sure about your edit. You turned it into a good question, but you did completely change the question. The answer to the original question is that it's UB and you cannot really predict the output. – klutt May 27 '21 at 10:30
  • @klutt: The original question was “When we return (*X)++, should not the function execution stop when we return (*X), so the ++ is not executed?” That is clearly about `return (*X)++` and not about the other code. – Eric Postpischil May 27 '21 at 10:31
  • @PaulHankin: Annex C is merely informative, not normative, and it may reflect the intent, but [the normative parts of the standard fail to state there is a sequence point after a `return` statement](https://stackoverflow.com/questions/15637678/sequence-point-after-a-return-statement). – Eric Postpischil May 27 '21 at 10:32
  • @EricPostpischil I'd say that what OP THOUGHT was the issue with the code. – klutt May 27 '21 at 10:33
  • @EricPostpischil I think it's a good thing you removed malloc and such, but I think you should put back `*X = (*X)++ + ++(*X)` and explain that it's UB. Preferably by linking to the canonical question about that. – klutt May 27 '21 at 10:34
  • @EricPostpischil I restored that line, just in case you want to update your answer for completeness. – klutt May 27 '21 at 10:43
  • @klutt: It is not a goal of Stack Overflow to debug other people’s code or to completely analyze programs and detail everything wrong with them. The main goal of Stack Overflow is to serve as a durable repository of questions and answers. OP’s intent in this question was clear; it was stated in the title and the body of the question. There is no reason to feed the unhealthy fixation on the “undefined behavior” resulting from the increment operators rather than other issues, such as the non-standard header or the non-standard `main` declaration… – Eric Postpischil May 27 '21 at 11:56
  • … OP can ask separately about other issues if they desire. – Eric Postpischil May 27 '21 at 11:56
  • 'university exam where we should predict the output' draw a couple lines through the question and write in upper case 'FIRE THE AUTHOR' - it is the only useful answer and deserves a top mark:) – Martin James May 27 '21 at 21:13

2 Answers2

1

The return statement causes its operand to be evaluated. Evaluation of an expression includes both computation of its value and of its side effects.

The C standard specifies the behavior of postfix ++ in 6.5.2.4 2:

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it)…

Similarly, if we wrote a = b++;, the assignment would not just assign the value and be done. The side effect occurs too.

The side effects are part of what an expression does, per 6.5 1:

An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof…

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

This:

*X = (*X)++ + ++(*X)

is undefined behavior, so you cannot really predict the result: Why are these constructs using pre and post-increment undefined behavior?

Also, if you want to know what undefined behavior is: Undefined, unspecified and implementation-defined behavior

This:

return (*X)++;

is equivalent to this: (Provided that X is an int)

int tmp = (*X)++;
return tmp;

return does not do anything magical here. I refer to @Eric's answer for a more in depth explanation of postfix operator.

When we return (*X)++, should not the function execution stop when we return (*X), so the ++ is not executed?

No

klutt
  • 30,332
  • 17
  • 55
  • 95