+ 2
What is the difference between i++ and ++i in C++ and C?
5 Answers
+ 2
i++ is post increment and it is same as i = i+1 .
++i is pre increment and it is same as i = i + 1
pre increment means first computation will be done and then assigned.and it exactly reverse when it comes to post increment
+ 3
int a = 0;
int b = ++a;// assign 'a' to 'b', then increment 'a' by 1.
// b = a, then a = a+1
int a = 0;
int b = a++;// increment 'a' by 1, then assign 'a' to 'b'.
// a = a+1, then b = a
+ 2
i++ use the variable first before incrementing the value its called postfix.
++i increment first before using the value its called prefix
+ 2
hello
++i and i++ both are unary operator
differences are as:
suppose;
int x= 0;
int y = ++x;//it means assign 'x' to 'y', then increment 'x' by 1.
// y = x, then x = x+1
int x = 0;
int y = x++;// means increment 'x' by 1, then assign 'x' to 'y'.
// x= x+1, then y= x
+ 1
hello,
it's both are the operator
++i is pre increment operator and i++ is a post increment operator
in ++i first i is incremented to i+1 and its value is fetched .
in i++ first value of i is fetched then i is incremented to i+1 after execution i.e. incremented value effect can be seen in next statement .
let us consider an example
where
int i=10;
int j =i++; //j will contain value of =10 and i will we incremented to 11
vs
int j = ++i; // i will be incremented first and then its value will be assigned to j i.e j will contain 11.
consider both statements as different .
I hope this will help to you