Increment and Decrement Operators:
Operators are the predefined symbols of the C library, and it is used to perform logical as well as mathematical operations to the operands. There are various types of operators in the C programming language, such as arithmetic, logical, bitwise, increment or decrement operators, etc.
Increment Operator in C
Increment Operators are the unary operators used to increment or add 1 to the operand value. The Increment operand is denoted by the double plus symbol (++). It has two types, Pre Increment and Post Increment Operators.
Pre-increment Operator
The pre-increment operator is used to increase the original value of the operand by 1 before assigning it to the expression.
Syntax:
- X = ++A;
In the above syntax, the value of operand ‘A’ is increased by 1, and then a new value is assigned to the variable ‘B’.
Post increment Operator
The post-increment operator is used to increment the original value of the operand by 1 after assigning it to the expression.
Syntax:
- X = A++;
In the above syntax, the value of operand ‘A’ is assigned to the variable ‘X’. After that, the value of variable ‘A’ is incremented by 1.
Decrement Operator in C
Decrement Operator is the unary operator, which is used to decrease the original value of the operand by 1. The decrement operator is represented as the double minus symbol (- -). It has two types, Pre Decrement and Post Decrement operators.
Pre Decrement Operator
The Pre Decrement Operator decreases the operand value by 1 before assigning it to the mathematical expression. In other words, the original value of the operand is first decreases, and then a new value is assigned to the other variable.
Syntax:
- B =- -A;
In the above syntax, the value of operand ‘A’ is decreased by 1, and then a new value is assigned to the variable ‘B’.
Post decrement Operator:
Post decrement operator is used to decrease the original value of the operand by 1 after assigning to the expression.
Syntax:
- B = A- -;
In the above syntax, the value of operand ‘A’ is assigned to the variable ‘B’, and then the value of A is decreased by 1.
Example:
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
return 0;
}
Output:
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000