Difference between preincrement and postincrement operator in c
Answers
Pre increment operator is used to increment variable value by 1 before assigning the value to the variable. Post increment operator is used to increment variable value by 1 after assigning the value to the variable.
Before we understand the difference between pre increment and post increment operator,let us see how they are denoted :-
++b is pre increment operator.
b++ is post increment operator.
The common thing between these two operator is that the value of b is increased by 1.
Pre-increment operator :-
Let us see notice these statements.
int a,b;
b = 6;
a = ++b;
What will be the value of a and b after the last statement ?
Notice that we have used pre increment operator. First the value of b is increased by 1(pre-increment) and then a is assigned the value of b.
Hence,
b = 7
a = 7
This is what this operator does, first the value is increased by 1 and then the value is assigned to the corresponding variable.
Post-increment operator :-
Let us see notice these statements.
int a,b;
b = 6;
a = b++;
What will be the value of a and b after the last statement ?
Notice that we have used post increment operator. First the value of a is assigned equal to b and then b is increased by 1.(post increment).
Hence,
a = 6
b = 7
This is what this operator does, first the value of the variable is assigned and then the the value of b is increased by 1.