What is the difference between prefix and postfix operators?

前端 未结 13 1531
天命终不由人
天命终不由人 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:31

    The postfix increment ++ does not increase the value of its operand until after it has been evaluated. The value of i++ is i.

    The prefix decrement increases the value of its operand before it has been evaluated. The value of --i is i - 1.

    Prefix increment/decrement change the value before the expression is evaluated. Postfix increment/decrement change the value after.

    So, in your case, fun(10) returns 10, and printing --i prints i - 1, which is 9.

    0 讨论(0)
  • 2020-11-22 08:33

    It has to do with the way the post-increment operator works. It returns the value of i and then increments the value.

    0 讨论(0)
  • 2020-11-22 08:34

    There is a big difference between postfix and prefix versions of ++.

    In the prefix version (i.e., ++i), the value of i is incremented, and the value of the expression is the new value of i.

    In the postfix version (i.e., i++), the value of i is incremented, but the value of the expression is the original value of i.

    Let's analyze the following code line by line:

    int i = 10;   // (1)
    int j = ++i;  // (2)
    int k = i++;  // (3)
    
    1. i is set to 10 (easy).
    2. Two things on this line:
      • i is incremented to 11.
      • The new value of i is copied into j. So j now equals 11.
    3. Two things on this line as well:
      • i is incremented to 12.
      • The original value of i (which is 11) is copied into k. So k now equals 11.

    So after running the code, i will be 12 but both j and k will be 11.

    The same stuff holds for postfix and prefix versions of --.

    0 讨论(0)
  • 2020-11-22 08:34

    Explanation:

    Step 1: int fun(int); Here we declare the prototype of the function fun().

    Step 2: int i = fun(10); The variable i is declared as an integer type and the result of the fun(10) will be stored in the variable i.

    Step 3: int fun(int i){ return (i++); } Inside the fun() we are returning a value return(i++). It returns 10. because i++ is the post-increement operator.

    Step 4: Then the control back to the main function and the value 10 is assigned to variable i.

    Step 5: printf("%d\n", --i); Here --i denoted pre-increement. Hence it prints the value 9.

    0 讨论(0)
  • 2020-11-22 08:35

    i++ is post increment. The increment takes place after the value is returned.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题