Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between prefix and postfix operators?

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 explain how this works?

#include<stdio.h> main() {     int i= fun(10);     printf("%d\n",--i); }  int fun (int i) {     return(i++); } 
like image 215
pradeep Avatar asked Aug 11 '11 18:08

pradeep


People also ask

What is the difference between prefix and postfix operator in C?

Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased). Prefix increment operator means the variable is incremented first and then the expression is evaluated using the new value of the variable.

What is postfix and prefix operators with example?

++b = 9. c-- = 8. --d = 7. In a++, postfix increment operator is used with 'a' which first printed the current value of 'a' (8) and then incremented it to 9. Similarly in ++b, the prefix operator first added one to the current value of 'b' thus making it 9 and then printed the incremented value.

What is the difference between prefix and postfix of -- and ++ operators in Java?

prefix vs postfix. In java, the prefix increment operator increases the value and then returns it to the variable. The prefix decrement operator will first decrement the value before returning it to the variable. The postfix increment operator returns the value to the variable before incrementing it.

What are prefix and postfix operators C++?

Increment ++ and Decrement -- Operator as Prefix and Postfix In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1. Simple enough till now.


2 Answers

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 --.

like image 136
Ken Wayne VanderLinde Avatar answered Sep 18 '22 08:09

Ken Wayne VanderLinde


Prefix:

int a=0;  int b=++a;          // b=1,a=1  

before assignment the value of will be incremented.

Postfix:

int a=0; int b=a++;  // a=1,b=0  

first assign the value of 'a' to 'b' then increment the value of 'a'

like image 25
Dilu Thankachan Avatar answered Sep 20 '22 08:09

Dilu Thankachan