What is the difference between prefix and postfix operators?

前端 未结 13 1802
天命终不由人
天命终不由人 2020-11-22 08:01

The following code prints a value of 9. Why? Here return(i++) will return a value of 11 and due to --i the value should be 10 itself, can anyone ex

13条回答
  •  旧巷少年郎
    2020-11-22 08:35

    First, note that the function parameter named i and the variable named i in main() are two different variables. I think that doesn't matter that much to the present discussion, but it's important to know.

    Second, you use the postincrement operator in fun(). That means the result of the expression is the value before i is incremented; the final value 11 of i is simply discarded, and the function returns 10. The variable i back in main, being a different variable, is assigned the value 10, which you then decrement to get 9.

提交回复
热议问题