Javatpoint Logo
Javatpoint Logo

Increment and Decrement Operators in C

Operators are the predefined symbols of the C/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 and Decrement Operators in C

Increment Operator

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

In the above syntax, the value of operand 'A' is increased by 1, and then a new value is assigned to the variable 'B'.

Example 1: Program to use the pre-increment operator in C

Output

Input the value of X: 10
Input the value of Y: 15
Input the value of Z: 20

The updated value of the X: 11
The updated value of the Y: 16
The updated value of the Z: 21

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

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.

Example 2: Program to use the post-increment operator in C

Output

Input the value of X: 10
Input the value of Y: 15
Input the value of Z: 20

The original value of a: 10
The original value of b: 15
The original value of c: 20

The updated value of the X: 11
The updated value of the Y: 16
The updated value of the Z: 21

Decrement Operator

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

In the above syntax, the value of operand 'A' is decreased by 1, and then a new value is assigned to the variable 'B'.

Example 3: Program to demonstrate the pre decrement operator in C

Output

Input the value of X: 5
Input the value of Y: 6
Input the value of Z: 7

The updated value of the X: 6
The updated value of the Y: 7
The updated value of the Z: 8

Post decrement Operator:

Post decrement operator is used to decrease the original value of the operand by 1 after assigning to the expression.

Syntax

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 4: Program to use the post decrement operator in C

Output

Input the value of X: 6
Input the value of Y: 12
Input the value of Z: 18

The original value of a: 6
The original value of b: 12
The original value of c: 18

The updated value of the X: 5
The updated value of the Y: 11
The updated value of the Z: 17

Example 5: Program to perform the pre increment and pre decrement operator

Output

Enter the value of i
5
Enter the value of j
10
After using the pre-incrementing, the value of i is 6
The value of x is 6
After using the pre-decrementing, the value of j is 9
The value of y is 9

Example 6: Program to print the post increment and post decrement operator

Output

Enter the value of i 10
Enter the value of j 20
After using the post-incrementing, the value of i is 11
The value of x is 10
After using the post-decrementing, the value of j is 19
The value of y is 20

Difference between the Increment and Decrement Operator in C

Increment Operator Decrement Operator
It is used to increment the value of a variable by 1. It is used to decrease the operand values by 1.
The increment operator is represented as the double plus (++) symbol. The decrement operator is represented as the double minus (--) symbol.
It has two types: pre-increment operator and post-increment operator. Similarly, it has two types: the pre-decrement operator and the post-decrement operator.
Pre increment operator means the value of the operator is incremented first and then used in the expression.
The post-increment operator means the operand is first used in the expression and then performs the increment operation to the original value by 1.
Pre decrement means the value of the operator is decremented first and then assigned in the expression.
Whereas the post decrement operator means the operand is first used in the expression and then performs the decrement operation to the operand's original value by 1.
Syntax for the pre increment operator: X = ++a;
Syntax for the post increment operator: X = a++;
Syntax for the pre decrement operator: X = --a;
Syntax for the post decrement operator: X = a--;
Both operators' works only to the single operand, not values. Both operators' works only to the single operand, not values.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA