What is the difference between prefix and postfix operators?

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

提交回复
热议问题