Increment and decrement operators
Increment and decrement operators in C programming are used to increase or decrease the value of a variable declared in a program by one. Increment and decrement operators are used as a shorthand notation to add or subtract one from the default value of any program variable.
So let’s learn about increment and decrement operators in C.
Increment (++) operator in c.
It increases the value of the default C program operand by one.
Increment operator can be used as prefix (++variable) or postfix (variable++) value increment.
When increment operator is used as prefix in a program. So by default the increment operation is used before using the value of program variable.
When increment operator is used as postfix. So this program applies increment operation after using the value of variable.
Increment (++) operator example.
int p = 1;
int q;
q = ++p; // Prefix increment operator – variable value p is incremented by 2 before assigning it to q, so q becomes 2
int p = 1;
int q;
q = p++; // Postfix increment – variable p is first assigned to q, (q becomes 2), then p is incremented, so p becomes 2
Decrement (–) operator in C.
Decrements the value of operand of the current program variable value by one.
Similar to increment operator, decrement operator can be used as prefix (–variable) or postfix (variable–).
Decrement (–) operator example.
int p = 2;
int q;
q = –p; // prefix decrement – p is decremented to 2 before being assigned to q, so q becomes 1
int p = 2;
int q;
q = p–; // postfix decrement – p is first assigned to q (q becomes 2), then p is decremented, so p becomes 1
Using assignment operators.
Increment and decrement operators in C language are often used in program loops to iterate over a range of program values.
They can also be used in program expressions for concise manipulation of program variables.
Be careful when using increment or decrement operators multiple times in the same program statement.