+ 4
What is prefix and postfix?
4 Réponses
+ 3
adds 1 to variable
++x prefix
x++ postfix
example
prefix adds 1 to x then copy value to y
x=4
y=++x
//x=5 y=5
postfix copy x value to y then adds 1 to y
x=4
y=x++
//x=4 y=5
works with + - / *
+ 1
operators are written before the operand is known as prefix.
eg. *AB(multiply A and B)
operators are written after the operand is known as postdoc.
eg. AB+(Add A, B)
0
There are increment operator (++) or decrement operator(--).
As you can see below:-
If you put increment operator before a variable.
e.g: ++a;
Then, it is an example of prefix.
Else, if you put increment operator after a variable .
e.g: a++;
Then, this will become an example of postfix.
NOTE: Same is for decrement operator(--) also.
- 1
prefix and postfix are unary operators in c++ when you write i++ in loop it is known as postfix notation and when you write ++i it is known as prefix notation eg:- for (int i=0;i <9;i++) it is(i++) is postfix notation and you can use them at other places also to increment decrement value of variables etc.