0
[DUPLICATE] What is the diffrence between x++; and ++x; ?
I don't know exactly how it differs.
4 Answers
+ 5
++x increments the value of x and then returns x.
x++ returns the value of x and then increments.
+ 1
for x++
its work like if;ex:
we have
int x=1, y=0;
y=x++;
then; value of y = 1
(it's work like y=x then x=x+1)
----------------
for ++x
its work like if;ex:
we have
int x=1, y=0;
y=++x;
then; value of y = 2
(it's work like x=x+1 then y=x)
in both cases x becomes 2
+ 1
x++ i.e post increment operator first assigns value then increments it.
++x i.e pre increment operator first increments value and then assigns it .
a=5
b=a++
here, we get ,a=6 , b=5
a=5
b=++a
here, we get ,a=6 , b=6